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 aoc_runner_derive::{aoc, aoc_generator};
use grid::{Coord2d, Grid, MirrorAxis}; use grid::{Coord2d, Grid, MirrorAxis};
use indicatif::{ProgressBar, ProgressStyle};
use itertools::Itertools; use itertools::Itertools;
use rayon::iter::IntoParallelRefIterator;
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt::Display; use std::fmt::Display;
use std::iter::repeat_n; use std::iter::repeat_n;
@@ -122,14 +124,14 @@ impl PresentsProblem {
t: ChristmasTree, t: ChristmasTree,
cur: usize, cur: usize,
cache: &mut HashSet<(usize, Grid<CellType>)>, // impossible shapes cache: &mut HashSet<(usize, Grid<CellType>)>, // impossible shapes
) -> Option<ChristmasTree> { ) -> bool {
if cur == t.presents.len() { if cur == t.presents.len() {
// done // done
return Some(t); return true;
} }
if cache.contains(&(cur, t.area.clone())) { if cache.contains(&(cur, t.area.clone())) {
// doomed // doomed
return None; return false;
} }
let variants = &self.shapes[t.presents[cur].shape].variants; let variants = &self.shapes[t.presents[cur].shape].variants;
for (i, _v) in variants.iter().enumerate() { for (i, _v) in variants.iter().enumerate() {
@@ -144,15 +146,15 @@ impl PresentsProblem {
}, },
i, i,
) { ) {
if let Some(solution) = self.place_next(new_t, cur + 1, cache) { if self.place_next(new_t, cur + 1, cache) {
return Some(solution); return true;
} }
} }
} }
} }
} }
cache.insert((cur, t.area.clone())); cache.insert((cur, t.area.clone()));
None false
} }
} }
@@ -161,7 +163,8 @@ fn part1(input: &PresentsProblem) -> u64 {
let input = input.clone(); let input = input.clone();
let mut count = 0; 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 area = (t.area.width() * t.area.height()) as u64;
let (occupied_area, present_count) = t let (occupied_area, present_count) = t
.presents .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)) .reduce(|(o_a, pc_a), (o_b, pc_b)| (o_a + o_b, pc_a + pc_b))
.unwrap(); .unwrap();
if occupied_area > area { if occupied_area > area {
let elapsed = timer.elapsed();
println!("{i} - CULLED IMPOSSIBLE ({}ms)", elapsed.as_millis());
// definitely impossible // definitely impossible
continue; continue;
} }
let available_3x3s = (t.area.width() / 3) * (t.area.height() / 3); let available_3x3s = (t.area.width() / 3) * (t.area.height() / 3);
if available_3x3s >= present_count { if available_3x3s >= present_count {
let elapsed = timer.elapsed();
println!("{i} - CULLED POSSIBLE ({}ms)", elapsed.as_millis());
// definitely_possible // definitely_possible
count += 1; count += 1;
continue; continue;
} }
if input if input.place_next(t.clone(), 0, &mut HashSet::new()) {
.place_next(t.clone(), 0, &mut HashSet::new()) let elapsed = timer.elapsed();
.is_some() println!("{i} - SOLVED POSSIBLE ({}ms)", elapsed.as_millis());
{
count += 1; count += 1;
continue; continue;
} }
let elapsed = timer.elapsed();
println!("{i} - SOLVED IMPOSSIBLE ({}ms)", elapsed.as_millis());
} }
count count
@@ -235,8 +244,29 @@ mod tests {
12x5: 1 0 1 0 2 2 12x5: 1 0 1 0 2 2
12x5: 1 0 1 0 3 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] #[rstest]
#[case(EXAMPLE, 2)] #[case(EXAMPLE, 2)]
#[case(EXAMPLE2, 3)]
fn part1_example(#[case] input: &str, #[case] expected: u64) { fn part1_example(#[case] input: &str, #[case] expected: u64) {
assert_eq!(part1(&parse(input)), expected); assert_eq!(part1(&parse(input)), expected);
} }
+1
View File
@@ -412,6 +412,7 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
} }
} }
// Find the first instance of `needle` in self in storage order (row then col)
pub fn find(&self, needle: &T) -> Option<Coord2d> { pub fn find(&self, needle: &T) -> Option<Coord2d> {
self.coord( self.coord(
self.data self.data