diff --git a/src/day12.rs b/src/day12.rs new file mode 100644 index 0000000..1226a34 --- /dev/null +++ b/src/day12.rs @@ -0,0 +1,105 @@ +#[aoc(day12, part1)] +pub fn part1(input: &str) -> u32 { + let (presents, under_trees) = parse(input); + under_trees + .iter() + .map(|ut| { + ((ut.presents + .iter() + .zip(presents.clone()) + .map(|(num, shape)| num * shape.iter().sum::()) + .sum::() + < (ut.width * ut.height)) + && (ut.width * ut.height >= ut.presents.iter().sum::() * 9)) as u32 + }) + .sum() +} + +type Present = Vec; + +#[derive(Debug)] +struct UnderTree { + width: u32, + height: u32, + presents: Vec, +} + +fn parse_under_tree(line: &str) -> UnderTree { + let mut splits = line.split(&['x', ':']); + let width = splits.next().unwrap().parse::().unwrap(); + let height = splits.next().unwrap().parse::().unwrap(); + let presents = splits + .next() + .unwrap() + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()) + .collect(); + UnderTree { + width: width, + height: height, + presents: presents, + } +} + +fn parse(input: &str) -> (Vec, Vec) { + ( + input + .lines() + .filter(|l| !l.chars().next().unwrap_or('a').is_digit(10)) + .map(|l| l.chars().map(|x| (x == '#') as u32).collect::>()) + .collect(), + input + .rsplit_once("\n\n") + .unwrap() + .1 + .lines() + .map(|l| parse_under_tree(l)) + .collect(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT_EXAMPLE: &str = "\ +0: +### +##. +##. + +1: +### +##. +.## + +2: +.## +### +##. + +3: +##. +### +##. + +4: +### +#.. +### + +5: +### +.#. +### + +4x4: 0 0 0 0 2 0 +12x5: 1 0 1 0 2 2 +12x5: 1 0 1 0 3 2 +"; + + #[test] + fn test_part1() { + assert_eq!(part1(INPUT_EXAMPLE), 2); + } +} diff --git a/src/lib.rs b/src/lib.rs index 97a7339..5d78afe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,12 +7,13 @@ pub mod day1; pub mod day2; // pub mod day10; pub mod day11; +pub mod day12; pub mod day3; pub mod day4; pub mod day5; pub mod day6; pub mod day7; pub mod day8; -// pub mod day9; +pub mod day9; aoc_lib! { year = 2025 }