Skip to content

Commit 47bec2e

Browse files
committed
[2024] Day 15 minor cleanup
1 parent f97266c commit 47bec2e

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

aoc_2024/src/day_15.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::fmt::{Debug, Write};
2-
31
use aoc_lib::{direction::cardinal::Direction, matrix::Matrix};
42
use common::{solution, Answer};
53
use nd_vec::{vector, Vec2};
@@ -46,18 +44,22 @@ impl Problem {
4644
_ => panic!(),
4745
});
4846

47+
// For part B, double the width of the board and convert the previously
48+
// one tile boxes into a Tile::Box and Tile::BoxRight
4949
if part_b {
5050
board.size = vector!(board.size.x() * 2, board.size.y());
51-
let mut new = Vec::new();
52-
for data in board.data {
53-
new.push(data);
54-
new.push(match data {
55-
Tile::Box => Tile::BoxRight,
56-
Tile::Robot => Tile::Empty,
57-
_ => data,
58-
});
51+
let mut i = 0;
52+
while i < board.data.len() {
53+
board.data.insert(
54+
i + 1,
55+
match board.data[i] {
56+
Tile::Box => Tile::BoxRight,
57+
Tile::Robot => Tile::Empty,
58+
x => x,
59+
},
60+
);
61+
i += 2;
5962
}
60-
board.data = new;
6163
}
6264

6365
let instructions = instructions
@@ -86,9 +88,7 @@ impl Problem {
8688
}
8789

8890
fn tick_all(&mut self, part_b: bool) {
89-
for _ in 0..self.instructions.len() {
90-
self.tick(part_b);
91-
}
91+
(0..self.instructions.len()).for_each(|_| self.tick(part_b));
9292
}
9393

9494
fn tick(&mut self, part_b: bool) {
@@ -107,6 +107,14 @@ impl Problem {
107107
}
108108
}
109109

110+
fn score(&self) -> u32 {
111+
self.board
112+
.iter()
113+
.filter(|x| *x.1 == Tile::Box)
114+
.map(|(pos, _)| (100 * pos.y() + pos.x()) as u32)
115+
.sum()
116+
}
117+
110118
// -> was successful
111119
fn push(&mut self, pos: Vec2<usize>, dir: Direction) -> bool {
112120
// if we are air, return true
@@ -132,6 +140,9 @@ impl Problem {
132140
}
133141
}
134142

143+
// these next two function are an absolute disaster, but im too tired to
144+
// clean them up right now...
145+
135146
fn can_push(&self, pos: Vec2<usize>, dir: Direction) -> bool {
136147
// println!("{pos:?}, {dir:?}");
137148
let value = self.board[pos];
@@ -201,34 +212,6 @@ impl Problem {
201212
false
202213
}
203214
}
204-
205-
fn score(&self) -> u32 {
206-
let mut score = 0;
207-
208-
for (pos, _) in self.board.iter().filter(|x| *x.1 == Tile::Box) {
209-
score += (100 * pos.y() + pos.x()) as u32;
210-
}
211-
212-
score
213-
}
214-
215-
fn debug(&self) {
216-
let mut dbg = self.board.clone();
217-
dbg.set(self.pos, Tile::Robot);
218-
println!("{:?}", dbg);
219-
}
220-
}
221-
222-
impl Debug for Tile {
223-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
224-
match self {
225-
Tile::Robot => f.write_char('@'),
226-
Tile::Wall => f.write_char('#'),
227-
Tile::Box => f.write_char('['),
228-
Tile::BoxRight => f.write_char(']'),
229-
Tile::Empty => f.write_char('.'),
230-
}
231-
}
232215
}
233216

234217
#[cfg(test)]

aoc_lib/src/matrix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use nd_vec::{vector, Vec2};
44
use num_traits::{Num, ToPrimitive};
55

66
pub struct Matrix<T> {
7-
data: Vec<T>,
7+
pub data: Vec<T>,
88
pub size: Vec2<usize>,
99
}
1010

@@ -43,7 +43,7 @@ impl<T> Matrix<T> {
4343
pos.x() < self.size.x() && pos.y() < self.size.y()
4444
}
4545

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

0 commit comments

Comments
 (0)