diff --git a/Cargo.lock b/Cargo.lock index 1de9713..1538659 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -355,5 +355,6 @@ version = "0.1.0" dependencies = [ "advent_core", "macros", + "regex", "utils", ] diff --git a/years/2024/Cargo.toml b/years/2024/Cargo.toml index 9c567dd..22dfdbb 100644 --- a/years/2024/Cargo.toml +++ b/years/2024/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] advent_core = { path = "../../advent_core" } macros = { path = "../../macros" } +regex = "1.11.1" utils = { path = "../../utils" } diff --git a/years/2024/src/day_3.rs b/years/2024/src/day_3.rs index 8ae384b..1dfdf4f 100644 --- a/years/2024/src/day_3.rs +++ b/years/2024/src/day_3.rs @@ -1,17 +1,31 @@ use advent_core::{Day, day_stuff, ex_for_day}; +use regex::RegexBuilder; pub struct Day3; +const RE: &str = r"mul\((\d+),(\d+)\)"; + +fn re_do(i: &str) -> u64 { + let re = RegexBuilder::new(RE).multi_line(true).build().unwrap(); + re.captures_iter(i).map(|c| { + c.get(1).unwrap().as_str().parse::().unwrap() + * + c.get(2).unwrap().as_str().parse::().unwrap() + }).sum::() +} + impl Day for Day3 { - day_stuff!(3, "", ""); + day_stuff!(3, "161", "48"); - fn part_1(_input: Self::Input) -> Option { - None + fn part_1(input: Self::Input) -> Option { + Some(re_do(&input).to_string()) } - fn part_2(_input: Self::Input) -> Option { - None + fn part_2(input: Self::Input) -> Option { + Some(input.split("do()").map(|v| + re_do(v.split("don't()").nth(0).unwrap()) + ).sum::().to_string()) } } diff --git a/years/2024/src/examples/day_3/1.txt b/years/2024/src/examples/day_3/1.txt index e69de29..2e1a90a 100644 --- a/years/2024/src/examples/day_3/1.txt +++ b/years/2024/src/examples/day_3/1.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) \ No newline at end of file diff --git a/years/2024/src/examples/day_3/2.txt b/years/2024/src/examples/day_3/2.txt index e69de29..b774ec9 100644 --- a/years/2024/src/examples/day_3/2.txt +++ b/years/2024/src/examples/day_3/2.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) \ No newline at end of file