diff --git a/src/bin/day08.rs b/src/bin/day08.rs
index d74c35a..da280d6 100644
--- a/src/bin/day08.rs
+++ b/src/bin/day08.rs
@@ -23,11 +23,11 @@ fn count_in(trees: &Input, counted: &mut Visited, swap: bool) {
pos = pos.swap();
}
- if *index(trees, &pos) > tallest {
+ if trees[pos] > tallest {
counted.insert(pos);
}
- max(tallest, *index(trees, &pos))
+ max(tallest, trees[pos])
}
(0..trees.len()).for_each(|y| {
@@ -49,14 +49,14 @@ fn count_in_columns(trees: &Input, counted: &mut Visited) {
}
fn count_visible(trees: &Input, position: SignedPosition, diff: SignedPosition) -> usize {
- let max_height = *index(trees, &position);
+ let max_height = trees[position];
let mut visible = 0;
let mut d = 1;
while in_range(trees, &(position + diff * d)) {
visible += 1;
- if *index(trees, &(position + diff * d)) >= max_height {
+ if trees[position + diff * d] >= max_height {
break;
}
@@ -83,31 +83,32 @@ struct Day08;
impl Solution for Day08 {
fn parse_input>(pathname: P) -> Input {
file_to_string(pathname)
- .lines()
- .map(|line| {
- line.chars()
- .map(|c| c.to_digit(10).unwrap() as i8)
- .collect_vec()
- })
- .collect_vec()
+ .lines()
+ .map(|line| {
+ line.chars()
+ .map(|c| c.to_digit(10).unwrap() as i8)
+ .collect_vec()
+ })
+ .collect_vec()
}
fn part_1(input: &Input) -> Output {
let mut counted = Visited::new();
- count_in_rows(input, &mut counted);
- count_in_columns(input, &mut counted);
+ count_in_rows(input, &mut counted);
+ count_in_columns(input, &mut counted);
- counted.len()
+ counted.len()
}
fn part_2(input: &Input) -> Output {
(0..input.len())
- .flat_map(|y| {
- (0..input[y].len()).map(move |x| compute_scenic_score(input, x as isize, y as isize))
- })
- .max()
- .unwrap()
+ .flat_map(|y| {
+ (0..input[y].len())
+ .map(move |x| compute_scenic_score(input, x as isize, y as isize))
+ })
+ .max()
+ .unwrap()
}
}
diff --git a/src/bin/day12.rs b/src/bin/day12.rs
index e457b60..b85d5b9 100644
--- a/src/bin/day12.rs
+++ b/src/bin/day12.rs
@@ -103,7 +103,7 @@ impl Solution for Day12 {
fn part_1(input: &Input) -> Output {
fn has_edge(graph: &[Vec], from: &Position, to: &Position) -> bool {
- get_elevation(*index(graph, to)) - get_elevation(*index(graph, from)) <= 1
+ get_elevation(graph[*to]) - get_elevation(graph[*from]) <= 1
}
let (start, target) = (find_start(input), find_target(input));
@@ -116,11 +116,11 @@ impl Solution for Day12 {
fn part_2(input: &Input) -> Output {
fn has_edge(graph: &[Vec], from: &Position, to: &Position) -> bool {
- (get_elevation(*index(graph, from)) - get_elevation(*index(graph, to))) <= 1
+ (get_elevation(graph[*from]) - get_elevation(graph[*to])) <= 1
}
fn is_target(graph: &[Vec], vertex: &Position) -> bool {
- get_elevation(*index(graph, vertex)) == 0
+ get_elevation(graph[*vertex]) == 0
}
let start = find_target(input);
diff --git a/src/vector2d.rs b/src/vector2d.rs
index b3aa335..4e87e5c 100644
--- a/src/vector2d.rs
+++ b/src/vector2d.rs
@@ -1,4 +1,5 @@
use std::cmp::Eq;
+use std::collections::VecDeque;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{Add, Index, IndexMut, Mul, Sub};
@@ -23,27 +24,42 @@ impl Vector2D {
}
}
-pub fn index<'a, C, I>(v: &'a [C], idx: &Vector2D) -> &'a C::Output
-where
- I: Copy,
- C: Index,
- usize: TryFrom,
- >::Error: Debug,
-{
- let (x, y): (usize, usize) = (idx.x.try_into().unwrap(), idx.y.try_into().unwrap());
- &v[y][x]
+macro_rules! generate_indices {
+ ($container:ty) => {
+ impl Index> for $container
+ where
+ I: Copy + TryInto,
+ >::Error: Debug,
+ C: Index,
+ {
+ type Output = C::Output;
+
+ fn index(&self, index: Vector2D) -> &Self::Output {
+ let (x, y): (usize, usize) =
+ (index.x.try_into().unwrap(), index.y.try_into().unwrap());
+ &self[y][x]
+ }
+ }
+
+ impl IndexMut> for $container
+ where
+ I: Copy + TryInto,
+ >::Error: Debug,
+ C: IndexMut,
+ {
+ fn index_mut(&mut self, index: Vector2D) -> &mut Self::Output {
+ let (x, y): (usize, usize) =
+ (index.x.try_into().unwrap(), index.y.try_into().unwrap());
+ &mut self[y][x]
+ }
+ }
+ };
}
-pub fn index_mut<'a, C, I>(v: &'a mut [C], idx: &Vector2D) -> &'a mut C::Output
-where
- I: Copy,
- C: IndexMut,
- usize: TryFrom,
- >::Error: Debug,
-{
- let (x, y): (usize, usize) = (idx.x.try_into().unwrap(), idx.y.try_into().unwrap());
- &mut v[y][x]
-}
+generate_indices!(VecDeque);
+generate_indices!([C]);
+generate_indices!(Vec);
+// generate_indices!([C; N], const N: usize);
pub fn in_range(v: &[Vec], idx: &Vector2D) -> bool
where