From 924cd1259448d11ddd87ed2e2da5468f9e2e11e1 Mon Sep 17 00:00:00 2001 From: Quentin Selle <62110608+quentinselle@users.noreply.github.com> Date: Sun, 8 Dec 2024 23:32:20 +0100 Subject: [PATCH] Add day6 part2 --- src/day6.rs | 131 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 106 insertions(+), 25 deletions(-) diff --git a/src/day6.rs b/src/day6.rs index 7061c46..13c7a04 100644 --- a/src/day6.rs +++ b/src/day6.rs @@ -1,4 +1,5 @@ use aoc_runner_derive::{aoc, aoc_generator}; +use std::collections::HashMap; #[derive(Debug)] pub struct Map { @@ -18,6 +19,7 @@ pub fn input_generator(input: &str) -> Map { } } +#[derive(PartialEq, Copy, Clone)] enum Direction { Up, Down, @@ -40,31 +42,110 @@ impl Direction { #[aoc(day6, part1)] pub fn part1(input: &Map) -> usize { let mut direction: Direction = Direction::Up; - let (mut y, mut x) = input.current; - let mut new_map = input.map.clone(); - let mut count = 0; - while y < input.map.len() && x < input.map[y].len() { - if input.map[y][x] == '#' { - // put back - match direction { - Direction::Up => y += 1, - Direction::Down => y -= 1, - Direction::Left => x += 1, - Direction::Right => x -= 1, - } + let mut visited = input.map.clone(); + let (mut row, mut col) = input.current; + let row_limit = input.map.len(); + let col_limit = input.map[0].len(); + let mut total: usize = 0; + loop { + let (next_row, next_col) = match direction { + Direction::Up if row > 0 => (row - 1, col), + Direction::Down if row < row_limit - 1 => (row + 1, col), + Direction::Left if col > 0 => (row, col - 1), + Direction::Right if col < col_limit - 1 => (row, col + 1), + _ => break, + }; + + if visited[next_row][next_col] == '#' { direction = direction.rotate(); - } else if new_map[y][x] != 'X' { - count += 1; - new_map[y][x] = 'X'; + } else if visited[next_row][next_col] == 'X' { + row = next_row; + col = next_col; + } else { + visited[next_row][next_col] = 'X'; + total += 1; + row = next_row; + col = next_col; } - match direction { - Direction::Up => y -= 1, - Direction::Down => y += 1, - Direction::Left => x -= 1, - Direction::Right => x += 1, + } + total +} + +#[aoc(day6, part2)] +pub fn part2(input: &Map) -> usize { + let mut total: usize = 0; + + let mut direction: Direction = Direction::Up; + let row_limit = input.map.len(); + let col_limit = input.map[0].len(); + + let (mut row, mut col) = input.current; + + loop { + let (next_row, next_col) = match direction { + Direction::Up if row > 0 => (row - 1, col), + Direction::Down if row < row_limit - 1 => (row + 1, col), + Direction::Left if col > 0 => (row, col - 1), + Direction::Right if col < col_limit - 1 => (row, col + 1), + _ => break, + }; + + if input.map[next_row][next_col] == '#' { + direction = direction.rotate(); + } else { + let mut loop_visited = input.map.clone(); + let mut loop_direction = direction; + loop_visited[next_row][next_col] = 'O'; + + let mut visited: HashMap<(usize, usize), Direction> = HashMap::new(); + + let (mut loop_row, mut loop_col) = (row, col); + + loop { + let (next_loop_row, next_loop_col) = match loop_direction { + Direction::Up if loop_row > 0 => (loop_row - 1, loop_col), + Direction::Down if loop_row < row_limit - 1 => (loop_row + 1, loop_col), + Direction::Left if loop_col > 0 => (loop_row, loop_col - 1), + Direction::Right if loop_col < col_limit - 1 => (loop_row, loop_col + 1), + _ => break, + }; + if loop_visited[next_loop_row][next_loop_col] == '#' + || loop_visited[next_loop_row][next_loop_col] == 'O' + { + loop_direction = loop_direction.rotate(); + } else { + loop_row = next_loop_row; + loop_col = next_loop_col; + loop_visited[next_loop_row][next_loop_col] = 'X'; + match visited.entry((loop_row, loop_col)) { + std::collections::hash_map::Entry::Vacant(entry) => { + entry.insert(loop_direction); + } + std::collections::hash_map::Entry::Occupied(entry) => { + if *entry.get() == loop_direction { + total += 1; + loop_visited[loop_row][loop_col] = 'T'; + for i in loop_visited { + for j in i { + print!("{j}"); + } + println!(); + } + println!(); + println!(); + break; + } + } + } + } + } + + // total += 1; + row = next_row; + col = next_col; } } - count + total } #[cfg(test)] @@ -87,8 +168,8 @@ mod tests { assert_eq!(41, part1(&input_generator(INPUT))) } - // #[test] - // fn test_part2() { - // assert_eq!(123, part2(&input_generator(INPUT))) - // } + #[test] + fn test_part2() { + assert_eq!(6, part2(&input_generator(INPUT))) + } }