From 47bec2e589a692e57e5f28f3dd7eaf054351fb34 Mon Sep 17 00:00:00 2001 From: Connor Slade Date: Sun, 15 Dec 2024 01:44:35 -0500 Subject: [PATCH] [2024] Day 15 minor cleanup --- aoc_2024/src/day_15.rs | 67 ++++++++++++++++-------------------------- aoc_lib/src/matrix.rs | 4 +-- 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/aoc_2024/src/day_15.rs b/aoc_2024/src/day_15.rs index 52cad9b..655ee63 100644 --- a/aoc_2024/src/day_15.rs +++ b/aoc_2024/src/day_15.rs @@ -1,5 +1,3 @@ -use std::fmt::{Debug, Write}; - use aoc_lib::{direction::cardinal::Direction, matrix::Matrix}; use common::{solution, Answer}; use nd_vec::{vector, Vec2}; @@ -46,18 +44,22 @@ impl Problem { _ => panic!(), }); + // For part B, double the width of the board and convert the previously + // one tile boxes into a Tile::Box and Tile::BoxRight if part_b { board.size = vector!(board.size.x() * 2, board.size.y()); - let mut new = Vec::new(); - for data in board.data { - new.push(data); - new.push(match data { - Tile::Box => Tile::BoxRight, - Tile::Robot => Tile::Empty, - _ => data, - }); + let mut i = 0; + while i < board.data.len() { + board.data.insert( + i + 1, + match board.data[i] { + Tile::Box => Tile::BoxRight, + Tile::Robot => Tile::Empty, + x => x, + }, + ); + i += 2; } - board.data = new; } let instructions = instructions @@ -86,9 +88,7 @@ impl Problem { } fn tick_all(&mut self, part_b: bool) { - for _ in 0..self.instructions.len() { - self.tick(part_b); - } + (0..self.instructions.len()).for_each(|_| self.tick(part_b)); } fn tick(&mut self, part_b: bool) { @@ -107,6 +107,14 @@ impl Problem { } } + fn score(&self) -> u32 { + self.board + .iter() + .filter(|x| *x.1 == Tile::Box) + .map(|(pos, _)| (100 * pos.y() + pos.x()) as u32) + .sum() + } + // -> was successful fn push(&mut self, pos: Vec2, dir: Direction) -> bool { // if we are air, return true @@ -132,6 +140,9 @@ impl Problem { } } + // these next two function are an absolute disaster, but im too tired to + // clean them up right now... + fn can_push(&self, pos: Vec2, dir: Direction) -> bool { // println!("{pos:?}, {dir:?}"); let value = self.board[pos]; @@ -201,34 +212,6 @@ impl Problem { false } } - - fn score(&self) -> u32 { - let mut score = 0; - - for (pos, _) in self.board.iter().filter(|x| *x.1 == Tile::Box) { - score += (100 * pos.y() + pos.x()) as u32; - } - - score - } - - fn debug(&self) { - let mut dbg = self.board.clone(); - dbg.set(self.pos, Tile::Robot); - println!("{:?}", dbg); - } -} - -impl Debug for Tile { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Tile::Robot => f.write_char('@'), - Tile::Wall => f.write_char('#'), - Tile::Box => f.write_char('['), - Tile::BoxRight => f.write_char(']'), - Tile::Empty => f.write_char('.'), - } - } } #[cfg(test)] diff --git a/aoc_lib/src/matrix.rs b/aoc_lib/src/matrix.rs index a1fdf87..3bf80ad 100644 --- a/aoc_lib/src/matrix.rs +++ b/aoc_lib/src/matrix.rs @@ -4,7 +4,7 @@ use nd_vec::{vector, Vec2}; use num_traits::{Num, ToPrimitive}; pub struct Matrix { - data: Vec, + pub data: Vec, pub size: Vec2, } @@ -43,7 +43,7 @@ impl Matrix { pos.x() < self.size.x() && pos.y() < self.size.y() } - pub fn iter(&self) -> impl Iterator, &T)> { + pub fn iter(&self) -> impl Iterator, &T)> { (0..self.data.len()).map(|x| { let pos = vector!(x % self.size.x(), x / self.size.x()); let data = self.get(pos).unwrap();