今天的解決方案比前一天稍微複雜一點,需要更多的程式碼,但仍然非常簡單。
解的基本概念:
第 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中文網其他相關文章!