Solve day 6

This commit is contained in:
hal8174 2024-12-06 13:47:16 +01:00
parent 9a7fa0dd65
commit 624b34d72e
3 changed files with 235 additions and 0 deletions

10
input/06-0.in Normal file
View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

130
input/06-1.in Normal file
View file

@ -0,0 +1,130 @@
#.............#.#...........................................................#..........#...........................#..........#...
............#......................#.......#..#.........#...................##....................................................
..............#...........#..#.......#.........#.#...........................#................................#......#............
.....#......................................#......#..#...................#....................#......................#...........
...#............................#................................#................................................................
..........#..................#.....#......................................................#.......................................
..#........#.......#.#...............................................................#......#.................................#...
.#....#................#................#.....#...................#...............................................#..#............
.......................................................................................#...#.#....#........##......#........#.#.##
............................................#.................#.....................#..........................#..................
....#..........##......................#.................................................................................#........
......#.............##.........#.......#.........#.......#.............#...#.......#..............................................
.#............................................................................#..........#....#...................................
...#..#....#..............................#...........................................................................#...........
.............#....................................................................................................................
................................#..................##...................................#...............................#......#..
..........#.........#.................#..........###...........................................#............#...#.................
.............#....................................#.......#.#.#............#............................................#.........
......#.#..............................................................................................#.........................#
...#...#.........#.........#..#......#................##.................#........................................................
.................................#......................#........#................#........................#......................
.......................##..............#................#................#..............................#.........................
.......#...#.....#.....##........................#..................##.....#......................................................
.............................................#...........................#..........................................#.....#.......
.........................#...............#....#..............#.......#...............#.#.................#.................#......
...........................................................#....#..................................#..............................
..........................#.................................................#............................##....#..................
.................#...#.........................................................#........................#.....#..#................
.........................#....#................................................#...#.....................#....#...........#.......
................................................##.......#.#....................................#.......##........................
..........##...#.......................#....#..#....................#........................#....................................
#......#.......................#............#.....................................................................#...............
.......#.......#...#..#..........#.......................................#.............#......#...................................
...............#............#..................#.................................................................................#
.......#...........#.......................##.....#.........#...........#...................#......#.........#.............##....#
...................#.........................#......#..........................................#..........#.........#......#......
.....#............#..............#...#......#....................#.............#..................................................
................................................#........#.....#...................#.....................................#.....#..
......................#.#...##......................##...............................................#.................#..........
#...................#.........................#...........#.....#................#.....................#...................#......
....#................#.....#.............#.....................................#....#...#.................................#.......
.........#.....................................................................................................#.#.......#........
.........#..........##..............................#............................#.#.............................#................
#....#.#..............................................##...#...#...............#.............#....................................
........#..#.#...................#.......................................................................#.............#..#.#...#.
.................................................................................................................#.......#........
............................................#..#...............##........#..........#.......................................#....#
........#...........................#............................#.....#........................#..........#...#..........#.......
.#...........#...................................................#...........................................#................#.#.
............#...............................#........#...........#..........#...........#.#..............#........................
...............................................................................##.#................#..............................
.#.......................#.#...........#..................................#.......................................................
..#..............................................##................#.......#....#..............#.....................#...#........
...................#.....#................................................#..........#.........#.........#.....................#..
........#................#................................................................................#......#................
...................#...#....#.........................#.........................#....................#..........................#.
.................#.......................................................#................................................#.......
..#...........................##.#.............#..#............................................#.......................#.#........
..#......#.........................................................................#.......................#......................
...........................................................................................................#............#......#..
............................................................^..........................................#..........#...............
..#.........#....................#..........................................#........#.........................................#..
........#...............#....#.............#......#.........................................................................#.....
.........#......#.....#..............#...........................................................#..............#.................
.........#...................................................#...###...........#.......#.##.......................................
.....................##.................#.........##.....................#................#................#.........#............
.......................................................................#..............#..............#.................#..........
.##.........#..........................................................................#....................................#.....
.........................#.............................#........................................................................#.
........#.............................................#......#....#.....................................#....................#....
........#...#..............................................................................#.........#............................
..............................#................................#..........#..............#..................................#.....
........##..#...................................#................................#.................#.........#......#..#........#.
..................#......#...................................................................#....................................
..........#.............#..............#........#............#...............#..#..........#..............#.#...#............#....
............................##....#..............#......................................................................#.........
...............................................#.....#...........#.....##...............#..#......................................
.........................#.......#...............................#...........#.........#...........#....#.........................
.#............#...#...............#..............................................................#..........................#....#
#...........#......................................................#.............................#......#.........................
..................................................#....#...............#.........................#................................
................................................#..........................#.......#..............................#....##.......#.
........#......#..#............#................#............................#......#.##.........#....................#..#........
.......................................................#..#.#....................#..#......................................#......
.....................#.......................................................................#............#.......................
.....................##........................#...................#.....................#........................................
..........................#...........................................................................................#...........
..#..........#...................#..............##..#.....................#...................#..............#...........#........
#................................#....##......#.............#..............#.......................#.........#...........#........
..................#................#...............................#.....#..........#................#..............#.............
................#.#.........#.....##...............................................................#..............#...............
..#...............................................................................................#...............................
..........#.................#...........#.......#.........................#...#.......#...........................................
..##....#......................................#.#.......................................#.................#......................
.......................#..#...#.##......................................................................#.........#...#...........
...............#.........#..............#.....................................................#...................#...............
......#.................#............#......#...................#..#.........................#........................#........#..
......................................................................#...........................#........#......................
..........#..........#........................#.............##....................................................................
....#................................................#.......................#................#...............#...................
.................#....................................................#......#....................................................
.#........#.......................................................................................................................
......##..................#....#......#.................#....................#.....................#..#...........#...............
#...#...#...#..............#........#.......................#.#..........#...............................#......#.................
#...#...................#.............#........#..........#...............................................................#...#...
..#................................#...#.........................#...................#......#.....................................
...#...###..........#............#...............................................................#..........#.....................
............#..............#.......#.....#..........#.....#.........................................................#............#
..#......................#..#...#................................#......#.......#.....#..................#.........#..............
....##......#.......................................................#..............................................#..............
...............................#.....#....#.................................................#.......#............#................
..........#....#......................#........#..........................#......#................................................
......#.............................#...........#..................#...........................#................................#.
...........................##..........................................#.....#.#.....................#................#...........
..#..............#.........................................................#........................#.........#.................#.
#.....#.....#......................#....................#........................................#...........#...........#......#.
.......................................#..#................................#.........#.......#..#.....#..........................#
..........#..............##...............#..........................................................#......#......#......#....#..
.............#..............#..#......#........#..#......##.......................#...........#.......#...........................
......................#.......................................#.......#..#.....................................#.........#...##...
...........................#...........................#......#....#.............................#.............................#..
..................#............................#....#........##.........................#..............#.............#............
.......#....................#.....#........#.......#.#...#...............#.....#.#.........................#.......#.#............
.....#.........#.......#..........#.................#..#...#..................................................##..................
................#........................................................#........................................................
....##......#........#..................#.........................#...................................#...#...................#...
..............#....................#.......#.......................#.............#......#..............#..........................
...........................#..............#.....#............#.....................#........#....#.......................#........
........#..##................#.....#.#..............#..............................#........................#.........#...........
.........................................................#...........................##..........#........#.##....................

95
src/bin/06.rs Normal file
View file

@ -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>]) -> 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::<Vec<_>>()
})
.collect::<Vec<_>>();
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);
}