day12
This commit is contained in:
105
src/day12.rs
Normal file
105
src/day12.rs
Normal file
@@ -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::<u32>())
|
||||
.sum::<u32>()
|
||||
< (ut.width * ut.height))
|
||||
&& (ut.width * ut.height >= ut.presents.iter().sum::<u32>() * 9)) as u32
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
type Present = Vec<u32>;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UnderTree {
|
||||
width: u32,
|
||||
height: u32,
|
||||
presents: Vec<u32>,
|
||||
}
|
||||
|
||||
fn parse_under_tree(line: &str) -> UnderTree {
|
||||
let mut splits = line.split(&['x', ':']);
|
||||
let width = splits.next().unwrap().parse::<u32>().unwrap();
|
||||
let height = splits.next().unwrap().parse::<u32>().unwrap();
|
||||
let presents = splits
|
||||
.next()
|
||||
.unwrap()
|
||||
.split_ascii_whitespace()
|
||||
.map(|x| x.parse::<u32>().unwrap())
|
||||
.collect();
|
||||
UnderTree {
|
||||
width: width,
|
||||
height: height,
|
||||
presents: presents,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> (Vec<Present>, Vec<UnderTree>) {
|
||||
(
|
||||
input
|
||||
.lines()
|
||||
.filter(|l| !l.chars().next().unwrap_or('a').is_digit(10))
|
||||
.map(|l| l.chars().map(|x| (x == '#') as u32).collect::<Vec<u32>>())
|
||||
.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);
|
||||
}
|
||||
}
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user