1
0
Fork 0
This commit is contained in:
Lars Martens 2024-12-03 07:49:46 +01:00
parent 3eb2bdb753
commit 9192d65846
Signed by: haselkern
GPG key ID: B5CF1F363C179AD4
4 changed files with 106 additions and 0 deletions

45
Cargo.lock generated
View file

@ -14,6 +14,15 @@ dependencies = [
"zerocopy", "zerocopy",
] ]
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "allocator-api2" name = "allocator-api2"
version = "0.2.20" version = "0.2.20"
@ -35,6 +44,7 @@ dependencies = [
"glam", "glam",
"itertools", "itertools",
"rayon", "rayon",
"regex",
] ]
[[package]] [[package]]
@ -188,6 +198,12 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.2" version = "1.20.2"
@ -232,6 +248,35 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.10.0" version = "0.10.0"

View file

@ -9,3 +9,4 @@ cached = "0.46.1"
glam = "0.29.2" glam = "0.29.2"
itertools = "0.12.0" itertools = "0.12.0"
rayon = "1.8.0" rayon = "1.8.0"
regex = "1.11.1"

1
input/03-test Normal file
View file

@ -0,0 +1 @@
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))

59
src/bin/03.rs Normal file
View file

@ -0,0 +1,59 @@
use aoc::*;
use regex::{Captures, Regex};
const INPUT: &str = include_str!("../../input/03");
fn main() {
assert_example!(part1, "03-test", 161);
println!("Part 1: {}", part1(INPUT));
assert_example!(part2, "03-test", 48);
println!("Part 2: {}", part2(INPUT));
}
fn part1(input: &str) -> i64 {
parse(input)
.into_iter()
.filter_map(|i| match i {
Instruction::Mul(a, b) => Some(a * b),
Instruction::Do | Instruction::Dont => None,
})
.sum()
}
fn part2(input: &str) -> i64 {
let mut sum = 0;
let mut enabled = true;
for instruction in parse(input) {
match instruction {
Instruction::Do => enabled = true,
Instruction::Dont => enabled = false,
Instruction::Mul(a, b) if enabled => {
sum += a * b;
}
Instruction::Mul(_, _) => (),
}
}
sum
}
fn parse(input: &str) -> Vec<Instruction> {
let regex = Regex::new(r#"(mul\((\d+),(\d+)\)|don't\(\)|do\(\))"#).unwrap();
regex.captures_iter(input).map(parse_instruction).collect()
}
fn parse_instruction(c: Captures) -> Instruction {
if c[0].starts_with("mul") {
let (a, b) = (c[2].parse().unwrap(), c[3].parse().unwrap());
Instruction::Mul(a, b)
} else if c[0].starts_with("don") {
Instruction::Dont
} else {
Instruction::Do
}
}
enum Instruction {
Do,
Dont,
Mul(i64, i64),
}