diff --git a/src/day06.rs b/src/day06.rs index be61f7e..23a09a1 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -64,7 +64,7 @@ fn parse(input: &str) -> (Grid, Coord) { } fn is_loop(m: &Grid, block: Coord, mut pos: Coord, mut dir: char) -> bool { - let mut visited = HashSet::new(); + let mut visited: Grid<[bool; 5]> = Grid::from_default(m.content_width, m.content_height); loop { let (x, y) = pos; @@ -79,10 +79,11 @@ fn is_loop(m: &Grid, block: Coord, mut pos: Coord, mut dir: char) -> bool return false; } else if next == block || '#' == m[next] { // we only check for loops on a collision to speed things up - if visited.contains(&(pos, dir)) { + // this is our own little hash function to speed things up + if visited[pos][dir as usize % 5] { return true; } - visited.insert((pos, dir)); + visited[pos][dir as usize % 5] = true; dir = match dir { '^' => '>', diff --git a/src/day14.rs b/src/day14.rs index 95e38a1..37595ac 100644 --- a/src/day14.rs +++ b/src/day14.rs @@ -69,7 +69,7 @@ fn part2(input: &str) -> i64 { let width = 101; let height = 103; let mut robots = parse(input); - let max_seconds = width * height as i64; + let max_seconds = width * height; for second in 0.. { if second > max_seconds { panic!("Seen all combinations but no christmas tree. So sad!"); diff --git a/src/utils/grid.rs b/src/utils/grid.rs index 2142c9a..c786c05 100644 --- a/src/utils/grid.rs +++ b/src/utils/grid.rs @@ -89,6 +89,15 @@ impl Grid { }) } + pub fn entries_mut(&mut self) -> impl Iterator { + self.content.iter_mut().enumerate().map(|(i, val)| { + ( + (i as i64 % self.content_width, i as i64 / self.content_width), + val, + ) + }) + } + pub fn map_values(self, f: fn(T) -> U) -> Grid { let new_content = self.content.into_iter().map(f).collect_vec(); Grid {