diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-16 21:01:26 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-16 21:01:26 -0500 |
commit | 813210ea84ed240a3bf3f81a53d3b972228757ae (patch) | |
tree | 41fdf9c8146b7d4538372cca47bb87e069501001 /src/2020 | |
parent | 7e3dfa7ba2cbca4cea878618d5c0c721c10b5264 (diff) | |
download | advent-of-code-813210ea84ed240a3bf3f81a53d3b972228757ae.tar.gz advent-of-code-813210ea84ed240a3bf3f81a53d3b972228757ae.zip |
lots of refactoring
Diffstat (limited to 'src/2020')
-rw-r--r-- | src/2020/3/mod.rs | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/src/2020/3/mod.rs b/src/2020/3/mod.rs index b702199..fb4e93d 100644 --- a/src/2020/3/mod.rs +++ b/src/2020/3/mod.rs @@ -1,40 +1,33 @@ -use anyhow::Context as _; +use crate::util::grid::*; struct Map { - grid: Vec<Vec<bool>>, + grid: Grid<bool>, } impl Map { - fn new(grid: Vec<Vec<bool>>) -> Self { + fn new(grid: Grid<bool>) -> Self { Self { grid } } fn rows(&self) -> usize { - self.grid.len() + self.grid.rows().0 } - fn tree_at(&self, x: usize, y: usize) -> anyhow::Result<bool> { + fn tree_at(&self, row: Row, col: Col) -> anyhow::Result<bool> { // unwrap safe because cycle().nth() can never fail - Ok(*self - .grid - .get(y) - .context("row too large")? - .iter() - .cycle() - .nth(x) - .unwrap()) + Ok(*self.grid[row].iter().cycle().nth(col.0).unwrap()) } fn trees_for_slope( &self, - x_incr: usize, - y_incr: usize, + row_incr: usize, + col_incr: usize, ) -> anyhow::Result<i64> { let mut trees = 0; - for r in 0..self.rows() / y_incr { - let x = r * x_incr; - let y = r * y_incr; - if self.tree_at(x, y)? { + for r in 0..self.rows() / row_incr { + let row = r * row_incr; + let col = r * col_incr; + if self.tree_at(Row(row), Col(col))? { trees += 1; } } @@ -43,17 +36,17 @@ impl Map { } pub fn part1() -> anyhow::Result<i64> { - let map = Map::new(data_bool_map!(b'#', b'.')); - map.trees_for_slope(3, 1) + let map = Map::new(data_bool_grid!(b'#', b'.')); + map.trees_for_slope(1, 3) } pub fn part2() -> anyhow::Result<i64> { - let map = Map::new(data_bool_map!(b'#', b'.')); + let map = Map::new(data_bool_grid!(b'#', b'.')); Ok(map.trees_for_slope(1, 1)? - * map.trees_for_slope(3, 1)? - * map.trees_for_slope(5, 1)? - * map.trees_for_slope(7, 1)? - * map.trees_for_slope(1, 2)?) + * map.trees_for_slope(1, 3)? + * map.trees_for_slope(1, 5)? + * map.trees_for_slope(1, 7)? + * map.trees_for_slope(2, 1)?) } #[test] |