From 0446debff449c0efa1007ce98779ede5acff90d9 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Wed, 2 Dec 2026 13:10:46 -0700 Subject: [PATCH] fixes --- src/day12.rs | 52 +++++++++++++++++++++++++++++++++++++---------- utils/grid/lib.rs | 1 + 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/day12.rs b/src/day12.rs index 96d30e8..a0af759 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -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)>, // impossible shapes - ) -> Option { + ) -> 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); } diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index 69205ad..815c1b5 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -412,6 +412,7 @@ impl Grid { } } + // Find the first instance of `needle` in self in storage order (row then col) pub fn find(&self, needle: &T) -> Option { self.coord( self.data