今天的挑战我第一次看到Regex时就尖叫起来,主要是因为每当我看到“提取该字符串的一部分”时,Regex就是我的首选;
基本概念和要求
所以我们需要找到所有 mul(number1, number2) 并将它们相乘,但忽略所有其他字符。
所以我们需要找到一种机制来查找所有有效的 mul() 函数声明。
为此,我们可以利用正则表达式的强大功能,使用以下模式
mul([0-9]{1,3},[0-9]{1,3})"
将匹配 mul( 0-9 之间的任何数字,1 > 3 的数字乘以右括号。
一旦我们获得了 mul() 匹配项,我们就可以再次利用正则表达式来提取数字,解析这些数字并添加到总数中。
非常简单直接的解决方案。
void Part1() { const string regex = @"mul\([0-9]{1,3},[0-9]{1,3}\)"; var matches = Regex.Matches(input, regex); var total = 0; foreach (Match match in matches) { var numbers = GetNumbers(match); total += numbers[0] * numbers[1]; } } int[] GetNumbers(Match match) { var numbers = Regex.Matches(match.Value, "\d{1,3}"); var a = int.Parse(numbers[0].Value); var b = int.Parse(numbers[1].Value); return [a, b]; }
这添加了稍微复杂的指令,添加了 do() 和 dont() 短语将启用或禁用 mil() 函数的警告。
处理这个问题的最佳方法似乎很简单,更新正则表达式模式以考虑 do() dont() 或 mul(number, number
正则表达式现在将使用 | 来查找这些短语中的任何一个。运算符。
然后我们可以循环遍历这些并使用 switch 语句来决定我们是否正在查看 do、dont 或 mil() 匹配,并相应地更新启用标志。
然后简单检查其 mul() 和 isEnabled 是否为 True,然后再相乘并添加到总数中。
以下两种解决方案的完整代码
using System.Text.RegularExpressions; var input = File.ReadAllText("./input1.txt"); // var input = @"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"; Part1(); Part2(); return; void Part1() { const string regex = @"mul\([0-9]{1,3},[0-9]{1,3}\)"; var matches = Regex.Matches(input, regex); var total = 0; foreach (Match match in matches) { var numbers = GetNumbers(match); total += numbers[0] * numbers[1]; } Console.WriteLine("Total: " + total); } void Part2() { const string regex = @"do\(\)|don't\(\)|mul\([0-9]{1,3},[0-9]{1,3}\)"; var matches = Regex.Matches(input, regex); // At the start, mul instructions are enabled var isEnabled = true; var total = 0; // loop over the matches (e.g do(), dont() or mul(x, y) foreach (Match match in matches) { switch (match.Value) { case "do()": isEnabled = true; break; case "don't()": isEnabled = false; break; default: { if (match.Value.StartsWith("mul") && isEnabled) { var numbers = GetNumbers(match); total += numbers[0] * numbers[1]; } break; } } } Console.WriteLine("Total: " + total); } int[] GetNumbers(Match match) { var numbers = Regex.Matches(match.Value, "\d{1,3}"); var a = int.Parse(numbers[0].Value); var b = int.Parse(numbers[1].Value); return [a, b]; }
如果您是我的系列的新手,我将重申,我正在使用 AoC '24 来帮助学习和提高我现有的 Python 技能 - 因此所有解决方案都将包括 C# 和 Python 尝试。
我们可以使用类似的概念,但利用 Python 语言和函数:
import re # Read input from file with open("./input1.txt", "r") as file: input_text = file.read() # Part 1 def part1(): regex = r"mul\(\d{1,3},\d{1,3}\)" matches = re.findall(regex, input_text) total = 0 for match in matches: a, b = get_numbers(match) total += a * b print(f"Total: {total}") # Part 2 def part2(): regex = r"do\(\)|don't\(\)|mul\(\d{1,3},\d{1,3}\)" matches = re.findall(regex, input_text) is_enabled = True # At the start, mul instructions are enabled total = 0 for match in matches: if match == "do()": is_enabled = True elif match == "don't()": is_enabled = False elif match.startswith("mul") and is_enabled: a, b = get_numbers(match) total += a * b print(f"Total: {total}") # Helper function to extract numbers from a match def get_numbers(match): numbers = re.findall(r"\d{1,3}", match) return int(numbers[0]), int(numbers[1]) # Execute parts part1() part2()
一如既往,您可以在 Twitter 上关注我,或者在 Github 上查看整个存储库以获取更多解决方案。
以上是AoC - Day 仔细考虑(C# 和 Python)的详细内容。更多信息请关注PHP中文网其他相关文章!