This commit is contained in:
ktims committed 2026-12-02 13:10:47 -07:00
1 parent 645f3683c9
commit 0446debff4
2 files changed
+42 -11

No files matched your search

+41 -11
View File
@@ -1,6 +1,8 @@
use aoc_runner_derive::{aoc, aoc_generator};
use grid::{Coord2d, Grid, MirrorAxis};
use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools;
use rayon::iter::IntoParallelRefIterator;
use std::collections::HashSet;
use std::fmt::Display;
use std::iter::repeat_n;
@@ -122,14 +124,14 @@ impl PresentsProblem {
t: ChristmasTree,
cur: usize,
cache: &mut HashSet<(usize, Grid<CellType>)>, // impossible shapes
) -> Option<ChristmasTree> {
) -> bool {
if cur == t.presents.len() {
// done
return Some(t);
return true;
}
if cache.contains(&(cur, t.area.clone())) {
// doomed
return None;
return false;
}
let variants = &self.shapes[t.presents[cur].shape].variants;
for (i, _v) in variants.iter().enumerate() {
@@ -144,15 +146,15 @@ impl PresentsProblem {
},
i,
) {
if let Some(solution) = self.place_next(new_t, cur + 1, cache) {
return Some(solution);
if self.place_next(new_t, cur + 1, cache) {
return true;
}
}
}
}
}
cache.insert((cur, t.area.clone()));
None
false
}
}
@@ -161,7 +163,8 @@ fn part1(input: &PresentsProblem) -> u64 {
let input = input.clone();
let mut count = 0;
for t in input.trees.iter() {
for (i, t) in input.trees.iter().enumerate() {
let timer = std::time::Instant::now();
let area = (t.area.width() * t.area.height()) as u64;
let (occupied_area, present_count) = t
.presents
@@ -170,22 +173,28 @@ fn part1(input: &PresentsProblem) -> u64 {
.reduce(|(o_a, pc_a), (o_b, pc_b)| (o_a + o_b, pc_a + pc_b))
.unwrap();
if occupied_area > area {
let elapsed = timer.elapsed();
println!("{i} - CULLED IMPOSSIBLE ({}ms)", elapsed.as_millis());
// definitely impossible
continue;
}
let available_3x3s = (t.area.width() / 3) * (t.area.height() / 3);
if available_3x3s >= present_count {
let elapsed = timer.elapsed();
println!("{i} - CULLED POSSIBLE ({}ms)", elapsed.as_millis());
// definitely_possible
count += 1;
continue;
}
if input
.place_next(t.clone(), 0, &mut HashSet::new())
.is_some()
{
if input.place_next(t.clone(), 0, &mut HashSet::new()) {
let elapsed = timer.elapsed();
println!("{i} - SOLVED POSSIBLE ({}ms)", elapsed.as_millis());
count += 1;
continue;
}
let elapsed = timer.elapsed();
println!("{i} - SOLVED IMPOSSIBLE ({}ms)", elapsed.as_millis());
}
count
@@ -235,8 +244,29 @@ mod tests {
12x5: 1 0 1 0 2 2
12x5: 1 0 1 0 3 2";
const EXAMPLE2: &str = "0:
.#.
###
.#.
1:
###
#..
###
2:
###
#.#
###
7x3: 1 2 0
17x17: 37 0 0
7x7: 1 0 4
7x7: 0 0 5";
#[rstest]
#[case(EXAMPLE, 2)]
#[case(EXAMPLE2, 3)]
fn part1_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part1(&parse(input)), expected);
}