diff --git a/input/06-0.in b/input/06-0.in new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/input/06-0.in @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/input/06-1.in b/input/06-1.in new file mode 100644 index 0000000..7ae77b7 --- /dev/null +++ b/input/06-1.in @@ -0,0 +1,130 @@ +#.............#.#...........................................................#..........#...........................#..........#... +............#......................#.......#..#.........#...................##.................................................... +..............#...........#..#.......#.........#.#...........................#................................#......#............ +.....#......................................#......#..#...................#....................#......................#........... +...#............................#................................#................................................................ +..........#..................#.....#......................................................#....................................... +..#........#.......#.#...............................................................#......#.................................#... +.#....#................#................#.....#...................#...............................................#..#............ +.......................................................................................#...#.#....#........##......#........#.#.## +............................................#.................#.....................#..........................#.................. +....#..........##......................#.................................................................................#........ +......#.............##.........#.......#.........#.......#.............#...#.......#.............................................. +.#............................................................................#..........#....#................................... +...#..#....#..............................#...........................................................................#........... +.............#.................................................................................................................... +................................#..................##...................................#...............................#......#.. +..........#.........#.................#..........###...........................................#............#...#................. +.............#....................................#.......#.#.#............#............................................#......... +......#.#..............................................................................................#.........................# +...#...#.........#.........#..#......#................##.................#........................................................ +.................................#......................#........#................#........................#...................... +.......................##..............#................#................#..............................#......................... +.......#...#.....#.....##........................#..................##.....#...................................................... +.............................................#...........................#..........................................#.....#....... +.........................#...............#....#..............#.......#...............#.#.................#.................#...... +...........................................................#....#..................................#.............................. +..........................#.................................................#............................##....#.................. +.................#...#.........................................................#........................#.....#..#................ +.........................#....#................................................#...#.....................#....#...........#....... +................................................##.......#.#....................................#.......##........................ +..........##...#.......................#....#..#....................#........................#.................................... +#......#.......................#............#.....................................................................#............... +.......#.......#...#..#..........#.......................................#.............#......#................................... +...............#............#..................#.................................................................................# +.......#...........#.......................##.....#.........#...........#...................#......#.........#.............##....# +...................#.........................#......#..........................................#..........#.........#......#...... +.....#............#..............#...#......#....................#.............#.................................................. +................................................#........#.....#...................#.....................................#.....#.. +......................#.#...##......................##...............................................#.................#.......... +#...................#.........................#...........#.....#................#.....................#...................#...... +....#................#.....#.............#.....................................#....#...#.................................#....... +.........#.....................................................................................................#.#.......#........ +.........#..........##..............................#............................#.#.............................#................ +#....#.#..............................................##...#...#...............#.............#.................................... +........#..#.#...................#.......................................................................#.............#..#.#...#. +.................................................................................................................#.......#........ +............................................#..#...............##........#..........#.......................................#....# +........#...........................#............................#.....#........................#..........#...#..........#....... +.#...........#...................................................#...........................................#................#.#. +............#...............................#........#...........#..........#...........#.#..............#........................ +...............................................................................##.#................#.............................. +.#.......................#.#...........#..................................#....................................................... +..#..............................................##................#.......#....#..............#.....................#...#........ +...................#.....#................................................#..........#.........#.........#.....................#.. +........#................#................................................................................#......#................ +...................#...#....#.........................#.........................#....................#..........................#. +.................#.......................................................#................................................#....... +..#...........................##.#.............#..#............................................#.......................#.#........ +..#......#.........................................................................#.......................#...................... +...........................................................................................................#............#......#.. +............................................................^..........................................#..........#............... +..#.........#....................#..........................................#........#.........................................#.. +........#...............#....#.............#......#.........................................................................#..... +.........#......#.....#..............#...........................................................#..............#................. +.........#...................................................#...###...........#.......#.##....................................... +.....................##.................#.........##.....................#................#................#.........#............ +.......................................................................#..............#..............#.................#.......... +.##.........#..........................................................................#....................................#..... +.........................#.............................#........................................................................#. +........#.............................................#......#....#.....................................#....................#.... +........#...#..............................................................................#.........#............................ +..............................#................................#..........#..............#..................................#..... +........##..#...................................#................................#.................#.........#......#..#........#. +..................#......#...................................................................#.................................... +..........#.............#..............#........#............#...............#..#..........#..............#.#...#............#.... +............................##....#..............#......................................................................#......... +...............................................#.....#...........#.....##...............#..#...................................... +.........................#.......#...............................#...........#.........#...........#....#......................... +.#............#...#...............#..............................................................#..........................#....# +#...........#......................................................#.............................#......#......................... +..................................................#....#...............#.........................#................................ +................................................#..........................#.......#..............................#....##.......#. +........#......#..#............#................#............................#......#.##.........#....................#..#........ +.......................................................#..#.#....................#..#......................................#...... +.....................#.......................................................................#............#....................... +.....................##........................#...................#.....................#........................................ +..........................#...........................................................................................#........... +..#..........#...................#..............##..#.....................#...................#..............#...........#........ +#................................#....##......#.............#..............#.......................#.........#...........#........ +..................#................#...............................#.....#..........#................#..............#............. +................#.#.........#.....##...............................................................#..............#............... +..#...............................................................................................#............................... +..........#.................#...........#.......#.........................#...#.......#........................................... +..##....#......................................#.#.......................................#.................#...................... +.......................#..#...#.##......................................................................#.........#...#........... +...............#.........#..............#.....................................................#...................#............... +......#.................#............#......#...................#..#.........................#........................#........#.. +......................................................................#...........................#........#...................... +..........#..........#........................#.............##.................................................................... +....#................................................#.......................#................#...............#................... +.................#....................................................#......#.................................................... +.#........#....................................................................................................................... +......##..................#....#......#.................#....................#.....................#..#...........#............... +#...#...#...#..............#........#.......................#.#..........#...............................#......#................. +#...#...................#.............#........#..........#...............................................................#...#... +..#................................#...#.........................#...................#......#..................................... +...#...###..........#............#...............................................................#..........#..................... +............#..............#.......#.....#..........#.....#.........................................................#............# +..#......................#..#...#................................#......#.......#.....#..................#.........#.............. +....##......#.......................................................#..............................................#.............. +...............................#.....#....#.................................................#.......#............#................ +..........#....#......................#........#..........................#......#................................................ +......#.............................#...........#..................#...........................#................................#. +...........................##..........................................#.....#.#.....................#................#........... +..#..............#.........................................................#........................#.........#.................#. +#.....#.....#......................#....................#........................................#...........#...........#......#. +.......................................#..#................................#.........#.......#..#.....#..........................# +..........#..............##...............#..........................................................#......#......#......#....#.. +.............#..............#..#......#........#..#......##.......................#...........#.......#........................... +......................#.......................................#.......#..#.....................................#.........#...##... +...........................#...........................#......#....#.............................#.............................#.. +..................#............................#....#........##.........................#..............#.............#............ +.......#....................#.....#........#.......#.#...#...............#.....#.#.........................#.......#.#............ +.....#.........#.......#..........#.................#..#...#..................................................##.................. +................#........................................................#........................................................ +....##......#........#..................#.........................#...................................#...#...................#... +..............#....................#.......#.......................#.............#......#..............#.......................... +...........................#..............#.....#............#.....................#........#....#.......................#........ +........#..##................#.....#.#..............#..............................#........................#.........#........... +.........................................................#...........................##..........#........#.##.................... diff --git a/src/bin/06.rs b/src/bin/06.rs new file mode 100644 index 0000000..7589294 --- /dev/null +++ b/src/bin/06.rs @@ -0,0 +1,95 @@ +use std::collections::HashSet; + +static DIR_MAP: [(isize, isize); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)]; + +fn find_loop(mut pos: (usize, usize), mut dir: usize, map: &[Vec]) -> bool { + let mut visited = HashSet::new(); + + visited.insert((pos, dir)); + + while let Some(next_pos) = Option::zip( + pos.0 + .checked_add_signed(DIR_MAP[dir].0) + .filter(|&x| x < map[0].len()), + pos.1 + .checked_add_signed(DIR_MAP[dir].1) + .filter(|&y| y < map[0].len()), + ) { + visited.insert((pos, dir)); + if map[next_pos.1][next_pos.0] { + dir = (dir + 1) % 4; + } else { + pos = next_pos; + } + if visited.contains(&(pos, dir)) { + return true; + } + } + + false +} + +fn main() { + let text = std::fs::read_to_string("input/06-1.in").unwrap(); + + let mut start_pos = (0, 0); + let mut dir = 0; + + let mut map = text + .lines() + .enumerate() + .map(|(y, l)| { + l.chars() + .enumerate() + .map(move |(x, c)| (x, y, c)) + .map(|(x, y, c)| match c { + '#' => true, + '.' => false, + '^' => { + start_pos = (x, y); + false + } + _ => unreachable!(), + }) + .collect::>() + }) + .collect::>(); + + let mut pos = start_pos; + let mut visited = HashSet::new(); + + visited.insert(pos); + + while let Some(next_pos) = Option::zip( + pos.0 + .checked_add_signed(DIR_MAP[dir].0) + .filter(|&x| x < map[0].len()), + pos.1 + .checked_add_signed(DIR_MAP[dir].1) + .filter(|&y| y < map[0].len()), + ) { + if map[next_pos.1][next_pos.0] { + dir = (dir + 1) % 4; + } else { + pos = next_pos; + visited.insert(pos); + } + } + + println!("{}", visited.len()); + + let mut count = 0; + for y in 0..map.len() { + for x in 0..map[0].len() { + if !map[y][x] { + map[y][x] = true; + if find_loop(start_pos, 0, &map) { + count += 1; + } + map[y][x] = false; + } + } + } + + println!("{}", count); +}