今天的解决方案比前一天稍微复杂一点,需要更多的代码,但仍然非常简单。
解决方案的基本概念:
第 1 部分必须满足以下条件才有效:
我们需要决定数字是否都在增加,或者逐个减少。
数字之间的差距不能超过3
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 < levels.Count - 1; i++) { var diff = Math.Abs(levels[i + 1] - levels[i]); if (diff is < 1 or > 3) { return false; } } return true; } bool IsIncreasing(List<int> numbers) { for (var i = 1; i < numbers.Count; i++) { if (numbers[i] < numbers[i - 1]) return false; } return true; } bool IsDecreasing(List<int> numbers) { for (var i = 1; i < numbers.Count; i++) { if (numbers[i] > numbers[i - 1]) return false; } return true; }
一个简单的解释是,在将报告传递给我们的助手 IsValidReport() 方法之前,我们使用 LINQ 将字符串数字解析为整数。
此方法检查所有数字是否在增加或减少。这是通过将 id 与有序列表进行比较来完成的,这将证明所有数字都朝一个方向发展。
一旦我们知道它们都在一个方向(而不是带),我们就可以检查每个数字是否彼此相差 3 个以内。使用 Math.Abs() 方法返回绝对数,帮助我们计算负数。
注意:绝对数,是数字和零之间的数字,即 -5 和 5 都会返回 5;
对于第 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); }
在这里,我们最大限度地发挥 LINQ 的功能,解析字符串报告,并使用它来修改 ints 数组。我们通过循环遍历数组并删除索引处的当前项来修改,从而修改了整数数组。
修改后,我们通过验证器运行修改后的报告。这可以使用 LINQ Select() 方法一次性完成,并结合 Any(),一旦满足条件(即修改后的报告有效),Any() 就会短路。
这是因为它规定如果可以删除任何一项,则报告可以被视为有效。它没有说明特定的项目,因此一旦我们找到匹配的项目(删除后意味着报告有效),我们就可以停止查看所有其他项目。
然后我们增加 validReports 计数器,以跟踪有效的报告。
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 < 1 or 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 def is_decreasing_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 < levels.Count - 1; i++) { var diff = Math.Abs(levels[i + 1] - levels[i]); if (diff is < 1 or > 3) { return false; } } return true; } bool IsIncreasing(List<int> numbers) { for (var i = 1; i < numbers.Count; i++) { if (numbers[i] < numbers[i - 1]) return false; } return true; } bool IsDecreasing(List<int> numbers) { for (var i = 1; i < numbers.Count; i++) { if (numbers[i] > numbers[i - 1]) return false; } return true; }
但我认为你可以同意,带有更好命名变量的详细方法更具可读性。
感谢您的阅读,明天再次加入我,并一如既往地在 Twitter 上关注我
以上是AoC Day 红鼻子报告(C# 和 Python)的详细内容。更多信息请关注PHP中文网其他相关文章!