搜索
首页后端开发Python教程AoC &#Day 红鼻子报告(C# 和 Python)

AoC

今天的解决方案比前一天稍微复杂一点,需要更多的代码,但仍然非常简单。

解决方案的基本概念:

第 1 部分必须满足以下条件才有效:

  • 我们需要决定数字是否都在增加,或者逐个减少。

  • 数字之间的差距不能超过3

第 1 部分

var reports = File.ReadAllLines("./input1.txt")
    .Select(x => x.Split(" "))
    .ToList();

Part1(reports);

void Part1(List<string> input)
{
    var validReports = input
        .Select(report => report.Select(int.Parse).ToList())
        .Count(IsValidReport);

    Console.WriteLine(validReports);
}

bool IsValidReport(List<int> levels)
{
    // We don't care about the numbers, just wether going up or down, not both
    var isIncreasing = IsIncreasing(levels);
    var isDecreasing = IsDecreasing(levels);

    if (!isIncreasing && !isDecreasing) return false;

    // Check that all adjacent levels differ by at least 1 and at most 3
    for (var i = 0; i  3)
        {
            return false;
        }
    }

    return true;
}

bool IsIncreasing(List<int> numbers)
{
    for (var i = 1; i  numbers)
{
    for (var i = 1; i  numbers[i - 1]) return false;
    }

    return true;
}

</int></int></string>

一个简单的解释是,在将报告传递给我们的助手 IsValidReport() 方法之前,我们使用 LINQ 将字符串数字解析为整数。

此方法检查所有数字是否在增加或减少。这是通过将 id 与有序列表进行比较来完成的,这将证明所有数字都朝一个方向发展。

一旦我们知道它们都在一个方向(而不是带),我们就可以检查每个数字是否彼此相差 3 个以内。使用 Math.Abs​​() 方法返回绝对数,帮助我们计算负数。

注意:绝对数,是数字和零之间的数字,即 -5 和 5 都会返回 5;

第2部分

对于第 2 部分,我们有稍微复杂的说明,但听起来比实际更难。

第二部分介绍了人为错误的警告,基本概念是您需要知道是否可以从报告中删除任何 1 项,并且该报告仍将被视为有效报告。

void Part2(List<string> input)
{
    var validReports = 0;

    foreach (var ints in input.Select(report => report.Select(int.Parse).ToList()))
    {
        // If valid as-is, count it
        if (IsValidReport(ints))
        {
            validReports++;
            continue;
        }

        // Check if removing any single level makes it valid
        if (ints.Select((t, i) => ints.Where((_, index) => index != i).ToList())
            .Any(IsValidReport))
        {
            validReports++;
        }
    }

    Console.WriteLine(validReports);
}
</string>

在这里,我们最大限度地发挥 LINQ 的功能,解析字符串报告,并使用它来修改 ints 数组。我们通过循环遍历数组并删除索引处的当前项来修改,从而修改了整数数组。

修改后,我们通过验证器运行修改后的报告。这可以使用 LINQ Select() 方法一次性完成,并结合 Any(),一旦满足条件(即修改后的报告有效),Any() 就会短路。

为什么只检查单个有效的修改报告?

这是因为它规定如果可以删除任何一项,则报告可以被视为有效。它没有说明特定的项目,因此一旦我们找到匹配的项目(删除后意味着报告有效),我们就可以停止查看所有其他项目。

然后我们增加 validReports 计数器,以跟踪有效的报告。

这是我将其转换为 Python 的尝试:

def main():
    # Read the input file and split each line into a list of strings
    with open("./input1.txt") as file:
        reports = [line.split() for line in file]

    part1(reports)
    part2(reports)


def part1(reports):
    # Count valid reports using a list comprehension and the IsValidReport function

    valid_reports = 0
    for report in reports:
        levels = list(map(int, report))

        if is_valid_report(levels):
            valid_reports += 1

    print(valid_reports)


def part2(reports):
    valid_reports = 0

    for report in reports:
        ints = list(map(int, report))

        # If valid as-is, count it
        if is_valid_report(ints):
            valid_reports += 1
            continue

        # Check if removing any single level makes it valid
        valid_with_dampener = False

        # Iterate over each element in ints
        for i in range(len(ints)):
            # Create a new list without the element at index i
            modified_report = [x for j, x in enumerate(ints) if j != i]

            # Check if the modified report is valid
            if is_valid_report(modified_report):
                valid_with_dampener = True
                break

        if valid_with_dampener:
            valid_reports += 1

    print(valid_reports)


def is_valid_report(levels):
    # Check if the sequence is either all increasing or all decreasing
    is_increasing = is_increasing_sequence(levels)
    is_decreasing = is_decreasing_sequence(levels)

    if not is_increasing and not is_decreasing:
        return False

    # Check that all adjacent levels differ by at least 1 and at most 3
    for i in range(len(levels) - 1):
        diff = abs(levels[i + 1] - levels[i])
        if diff  3:
            return False

    return True


def is_increasing_sequence(numbers):
    for i in range(1, len(numbers)):
        if numbers[i]  numbers[i - 1]:
            return False
    return True


if __name__ == "__main__":
    main()

对于不太冗长的解决方案,我们可以用修改和检查替换第 2 部分内部 for 循环,就像我们在 C# 解决方案中使用 LINQ 所做的那样,如下所示:

var reports = File.ReadAllLines("./input1.txt")
    .Select(x => x.Split(" "))
    .ToList();

Part1(reports);

void Part1(List<string> input)
{
    var validReports = input
        .Select(report => report.Select(int.Parse).ToList())
        .Count(IsValidReport);

    Console.WriteLine(validReports);
}

bool IsValidReport(List<int> levels)
{
    // We don't care about the numbers, just wether going up or down, not both
    var isIncreasing = IsIncreasing(levels);
    var isDecreasing = IsDecreasing(levels);

    if (!isIncreasing && !isDecreasing) return false;

    // Check that all adjacent levels differ by at least 1 and at most 3
    for (var i = 0; i  3)
        {
            return false;
        }
    }

    return true;
}

bool IsIncreasing(List<int> numbers)
{
    for (var i = 1; i  numbers)
{
    for (var i = 1; i  numbers[i - 1]) return false;
    }

    return true;
}

</int></int></string>

但我认为你可以同意,带有更好命名变量的详细方法更具可读性。

感谢您的阅读,明天再次加入我,并一如既往地在 Twitter 上关注我

以上是AoC &#Day 红鼻子报告(C# 和 Python)的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python中的合并列表:选择正确的方法Python中的合并列表:选择正确的方法May 14, 2025 am 12:11 AM

Tomergelistsinpython,YouCanusethe操作员,estextMethod,ListComprehension,Oritertools

如何在Python 3中加入两个列表?如何在Python 3中加入两个列表?May 14, 2025 am 12:09 AM

在Python3中,可以通过多种方法连接两个列表:1)使用 运算符,适用于小列表,但对大列表效率低;2)使用extend方法,适用于大列表,内存效率高,但会修改原列表;3)使用*运算符,适用于合并多个列表,不修改原列表;4)使用itertools.chain,适用于大数据集,内存效率高。

Python串联列表字符串Python串联列表字符串May 14, 2025 am 12:08 AM

使用join()方法是Python中从列表连接字符串最有效的方法。1)使用join()方法高效且易读。2)循环使用 运算符对大列表效率低。3)列表推导式与join()结合适用于需要转换的场景。4)reduce()方法适用于其他类型归约,但对字符串连接效率低。完整句子结束。

Python执行,那是什么?Python执行,那是什么?May 14, 2025 am 12:06 AM

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python:关键功能是什么Python:关键功能是什么May 14, 2025 am 12:02 AM

Python的关键特性包括:1.语法简洁易懂,适合初学者;2.动态类型系统,提高开发速度;3.丰富的标准库,支持多种任务;4.强大的社区和生态系统,提供广泛支持;5.解释性,适合脚本和快速原型开发;6.多范式支持,适用于各种编程风格。

Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具