From 9192d658468427bd3cc7cfeaa994c02a13bea29f Mon Sep 17 00:00:00 2001 From: Lars Martens Date: Tue, 3 Dec 2024 07:49:46 +0100 Subject: [PATCH] Solve 03 --- Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + input/03-test | 1 + src/bin/03.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 input/03-test create mode 100644 src/bin/03.rs diff --git a/Cargo.lock b/Cargo.lock index d72c3eb..bbb663d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.20" @@ -35,6 +44,7 @@ dependencies = [ "glam", "itertools", "rayon", + "regex", ] [[package]] @@ -188,6 +198,12 @@ dependencies = [ "either", ] +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + [[package]] name = "once_cell" version = "1.20.2" @@ -232,6 +248,35 @@ dependencies = [ "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]] name = "strsim" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index 2c594d7..1a442ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ cached = "0.46.1" glam = "0.29.2" itertools = "0.12.0" rayon = "1.8.0" +regex = "1.11.1" diff --git a/input/03-test b/input/03-test new file mode 100644 index 0000000..30032cb --- /dev/null +++ b/input/03-test @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) diff --git a/src/bin/03.rs b/src/bin/03.rs new file mode 100644 index 0000000..27f20b9 --- /dev/null +++ b/src/bin/03.rs @@ -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 { + 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), +}