Skip to content

Commit

Permalink
[2024] Day 15 minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 15, 2024
1 parent f97266c commit 47bec2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
67 changes: 25 additions & 42 deletions aoc_2024/src/day_15.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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<usize>, dir: Direction) -> bool {
// if we are air, return true
Expand All @@ -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<usize>, dir: Direction) -> bool {
// println!("{pos:?}, {dir:?}");
let value = self.board[pos];
Expand Down Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions aoc_lib/src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nd_vec::{vector, Vec2};
use num_traits::{Num, ToPrimitive};

pub struct Matrix<T> {
data: Vec<T>,
pub data: Vec<T>,
pub size: Vec2<usize>,
}

Expand Down Expand Up @@ -43,7 +43,7 @@ impl<T> Matrix<T> {
pos.x() < self.size.x() && pos.y() < self.size.y()
}

pub fn iter(&self) -> impl Iterator<Item = (Vec2<usize>, &T)> {
pub fn iter(&self) -> impl Iterator<Item = (Vec2<usize>, &T)> {
(0..self.data.len()).map(|x| {
let pos = vector!(x % self.size.x(), x / self.size.x());
let data = self.get(pos).unwrap();
Expand Down

0 comments on commit 47bec2e

Please sign in to comment.