
今天的挑战我第一次看到regex时就尖叫起来,主要是因为每当我看到“提取该字符串的一部分”时,regex就是我的首选;
基本概念和要求
所以我们需要找到所有 mul(number1, number2) 并将它们相乘,但忽略所有其他字符。
所以我们需要找到一种机制来查找所有有效的 mul() 函数声明。
立即学习“Python免费学习笔记(深入)”;
为此,我们可以利用正则表达式的强大功能,使用以下模式
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号