Skip to content

Commit 2b20f7d

Browse files
committed
Talk about Day 15
1 parent ae979ca commit 2b20f7d

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/day15.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@ use crate::{
66
};
77

88
pub fn solve(input: &str) -> (usize, usize) {
9-
let (mut warehouse, movements) = parse(input);
10-
let mut warehouse2 = warehouse.clone().into_part2();
11-
(
12-
part1(&mut warehouse, &movements),
13-
part2(&mut warehouse2, &movements),
14-
)
9+
let (warehouse, movements) = parse(input);
10+
let warehouse2 = warehouse.make_part2();
11+
(part1(warehouse, &movements), part2(warehouse2, &movements))
1512
}
1613

17-
fn part1(warehouse: &mut Warehouse, movements: &[Direction]) -> usize {
14+
fn part1(mut warehouse: Warehouse, movements: &[Direction]) -> usize {
1815
for &m in movements.into_iter() {
1916
warehouse.do_move(m)
2017
}
2118
warehouse.score()
2219
}
2320

24-
fn part2(warehouse: &mut Warehouse2, movements: &[Direction]) -> usize {
21+
fn part2(mut warehouse: Warehouse2, movements: &[Direction]) -> usize {
2522
for &m in movements.into_iter() {
2623
warehouse.do_move(m)
2724
}
@@ -100,14 +97,14 @@ impl Warehouse {
10097
.sum()
10198
}
10299

103-
fn into_part2(self) -> Warehouse2 {
100+
fn make_part2(&self) -> Warehouse2 {
104101
Warehouse2 {
105102
robot: self.robot.scaled_x(2),
106103
map: Grid::new(
107104
(self.map.size.0 * 2, self.map.size.1),
108105
self.map
109106
.elements
110-
.into_iter()
107+
.iter()
111108
.flat_map(|t| match t {
112109
Thing::Wall => [Thing2::Wall, Thing2::Wall],
113110
Thing::Box => [Thing2::BoxLeft, Thing2::BoxRight],
@@ -219,15 +216,13 @@ impl fmt::Display for Warehouse2 {
219216
write!(
220217
f,
221218
"{}",
222-
self.map.display(
223-
|i| if i == robot { Some('@') } else { None },
224-
|t| match t {
225-
Thing2::Wall => '#',
226-
Thing2::BoxLeft => '[',
227-
Thing2::BoxRight => ']',
228-
Thing2::Floor => '.',
229-
}
230-
)
219+
self.map.display(|t, i| match (t, i) {
220+
(_, i) if i == robot => '@',
221+
(Thing2::Wall, _) => '#',
222+
(Thing2::BoxLeft, _) => '[',
223+
(Thing2::BoxRight, _) => ']',
224+
(Thing2::Floor, _) => '.',
225+
})
231226
)?;
232227

233228
Ok(())

src/grid.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ impl<T> Grid<T> {
126126
dyadic(self.size, monadic)
127127
}
128128

129-
pub fn display<'a, O, M>(&'a self, overlay: O, mapping: M) -> GridDisplayer<'a, M, O, T>
129+
pub fn display<'a, M>(&'a self, mapping: M) -> GridDisplayer<'a, M, T>
130130
where
131-
O: Fn(Index) -> Option<char>,
132-
M: Fn(&T) -> char,
131+
M: Fn(&T, Index) -> char,
133132
{
134-
GridDisplayer::new(mapping, overlay, &self)
133+
GridDisplayer::new(mapping, &self)
135134
}
136135
}
137136

@@ -163,40 +162,33 @@ impl<M: FnMut(char) -> Result<T, GridParseError>, T> GridParser<M> {
163162
}
164163
}
165164

166-
pub struct GridDisplayer<'a, M, O, T> {
165+
pub struct GridDisplayer<'a, M, T> {
167166
mapping: M,
168-
overlay: O,
169167
grid: &'a Grid<T>,
170168
}
171169

172-
impl<'a, M, O, T> GridDisplayer<'a, M, O, T>
170+
impl<'a, M, T> GridDisplayer<'a, M, T>
173171
where
174-
M: Fn(&T) -> char,
175-
O: Fn(Index) -> Option<char>,
172+
M: Fn(&T, Index) -> char,
176173
{
177-
pub fn new(mapping: M, overlay: O, grid: &'a Grid<T>) -> Self {
178-
Self {
179-
mapping,
180-
overlay,
181-
grid,
182-
}
174+
pub fn new(mapping: M, grid: &'a Grid<T>) -> Self {
175+
Self { mapping, grid }
183176
}
184177
}
185178

186-
impl<'a, M, O, T> fmt::Display for GridDisplayer<'a, M, O, T>
179+
impl<'a, M, T> fmt::Display for GridDisplayer<'a, M, T>
187180
where
188181
// I would like to have this a FnMut (as it is Map),
189182
// but the fmt::Display-interface forbids that
190-
M: Fn(&T) -> char,
191-
O: Fn(Index) -> Option<char>,
183+
M: Fn(&T, Index) -> char,
192184
{
193185
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194186
for row in self.grid.iter_indices_by_rows().into_iter() {
195187
writeln!(
196188
f,
197189
"{}",
198190
row.into_iter()
199-
.map(|i| ((self.overlay)(i)).unwrap_or_else(|| (self.mapping)(&self.grid[i])))
191+
.map(|i| (self.mapping)(&self.grid[i], i))
200192
.collect::<String>()
201193
)?;
202194
}

0 commit comments

Comments
 (0)