diff options
Diffstat (limited to 'day3')
| -rw-r--r-- | day3/Cargo.toml | 8 | ||||
| -rw-r--r-- | day3/src/main.rs | 79 |
2 files changed, 87 insertions, 0 deletions
diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..cabf6e3 --- /dev/null +++ b/day3/Cargo.toml | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | [package] | ||
| 2 | name = "day3" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | aoc = { path = "../aoc" } | ||
| 8 | regex = "1.11.1" \ No newline at end of file | ||
diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..86ba39b --- /dev/null +++ b/day3/src/main.rs | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | |||
| 2 | use aoc; | ||
| 3 | use std::io::{BufReader, Read}; | ||
| 4 | use aoc::Day; | ||
| 5 | use regex::Regex; | ||
| 6 | |||
| 7 | struct Day3 {} | ||
| 8 | |||
| 9 | impl Day for Day3 { | ||
| 10 | fn example_input(&self) -> &'static str { | ||
| 11 | r#" | ||
| 12 | xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) | ||
| 13 | "#.trim() | ||
| 14 | } | ||
| 15 | |||
| 16 | fn example_input_part_2(&self) -> &'static str { | ||
| 17 | r#" | ||
| 18 | xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) | ||
| 19 | "#.trim() | ||
| 20 | } | ||
| 21 | |||
| 22 | fn example_result_part_1(&self) -> &'static str { | ||
| 23 | "161" | ||
| 24 | } | ||
| 25 | |||
| 26 | fn example_result_part_2(&self) -> &'static str { | ||
| 27 | "48" | ||
| 28 | } | ||
| 29 | |||
| 30 | fn part_1(&self, input: BufReader<Box<dyn Read>>) -> String { | ||
| 31 | let code = self.read_lines(input).iter().map(|l| l.trim()).collect::<Vec<&str>>().join(""); | ||
| 32 | let re = Regex::new(r"mul\((?P<a>\d+),(?P<b>\d+)\)").unwrap(); | ||
| 33 | re | ||
| 34 | .captures_iter(&code) | ||
| 35 | .map(|c| { | ||
| 36 | c.name("a").unwrap().as_str().parse::<u32>().unwrap() * c.name("b").unwrap().as_str().parse::<u32>().unwrap() | ||
| 37 | }) | ||
| 38 | .sum::<u32>() | ||
| 39 | .to_string() | ||
| 40 | } | ||
| 41 | |||
| 42 | fn part_2(&self, input: BufReader<Box<dyn Read>>) -> String { | ||
| 43 | let code = self.read_lines(input).iter().map(|l| l.trim()).collect::<Vec<&str>>().join(""); | ||
| 44 | println!("{}", code); | ||
| 45 | |||
| 46 | let mut mul = true; | ||
| 47 | let re = Regex::new(r"mul\((?P<a>\d+),(?P<b>\d+)\)|(?P<enable>do)\(\)|(?P<disable>don't)\(\)").unwrap(); | ||
| 48 | re | ||
| 49 | .captures_iter(&code) | ||
| 50 | .map(|c| { | ||
| 51 | |||
| 52 | if c.name("enable") != None { | ||
| 53 | mul = true; | ||
| 54 | } else if c.name("disable") != None { | ||
| 55 | mul = false; | ||
| 56 | } else if mul { | ||
| 57 | return c.name("a").unwrap().as_str().parse::<u32>().unwrap() * c.name("b").unwrap().as_str().parse::<u32>().unwrap(); | ||
| 58 | } | ||
| 59 | |||
| 60 | 0 | ||
| 61 | }) | ||
| 62 | .sum::<u32>() | ||
| 63 | .to_string() | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | fn main() { | ||
| 69 | aoc::main(&Day3 {}); | ||
| 70 | } | ||
| 71 | |||
| 72 | #[cfg(test)] | ||
| 73 | mod tests { | ||
| 74 | use super::*; | ||
| 75 | #[test] | ||
| 76 | fn test_day3() { | ||
| 77 | aoc::test_day(&Day3 {}); | ||
| 78 | } | ||
| 79 | } | ||
