Skip to content

Commit

Permalink
Factor out small functions
Browse files Browse the repository at this point in the history
jeffs committed Dec 11, 2023
1 parent 1c3290c commit 7eb06a2
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions 2023/day11/src/lib.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,21 @@ impl Tile {
}
}

fn offsets<'a, I, J>(tileses: I, expansion: usize) -> Vec<usize>
where
I: Iterator<Item = J>,
J: IntoIterator<Item = &'a Tile>,
{
tileses
.scan(0, |gap, tiles| {
if tiles.into_iter().all(Tile::is_empty) {
*gap = *gap + expansion - 1;
}
Some(*gap)
})
.collect()
}

pub struct Grid(Vec<Vec<Tile>>);

impl Grid {
@@ -32,6 +47,14 @@ impl Grid {
)
}

fn rows(&self) -> impl Iterator<Item = &Vec<Tile>> {
self.0.iter()
}

fn columns(&self) -> impl Iterator<Item = impl Iterator<Item = &Tile>> {
(0..self.width()).map(move |j| self.rows().map(move |row| &row[j]))
}

fn height(&self) -> usize {
self.0.len()
}
@@ -52,24 +75,8 @@ impl Grid {
}

fn galaxies(&self, expansion: usize) -> impl Iterator<Item = Position> + '_ {
let row_offsets: Vec<usize> = self
.0
.iter()
.scan(0, |gap, row| {
if row.iter().all(Tile::is_empty) {
*gap = *gap + expansion - 1;
}
Some(*gap)
})
.collect();
let column_offsets: Vec<usize> = (0..self.width())
.scan(0, |gap, j| {
if self.0.iter().all(|row| row[j].is_empty()) {
*gap = *gap + expansion - 1;
}
Some(*gap)
})
.collect();
let row_offsets = offsets(self.rows(), expansion);
let column_offsets = offsets(self.columns(), expansion);
self.enumerate()
.filter(move |(_, t)| !t.is_empty())
.map(move |(Position(i, j), _)| Position(i + row_offsets[i], j + column_offsets[j]))

0 comments on commit 7eb06a2

Please sign in to comment.