- commit
- f02078d42e1a016bb5519920522f02b9db0cb4d9
- parent
- 0b0c9de03fdf067506200d9be099411c07263872
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-12-07 12:28
2024-12-06
Diffstat
A | 2024/06/input.txt | 130 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/06/solution.rs | 128 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | 2024/06/test.txt | 10 | ++++++++++ |
3 files changed, 268 insertions, 0 deletions
diff --git a/2024/06/input.txt b/2024/06/input.txt
@@ -0,0 +1,130 @@ -1 1 ........#...........................#...........#............................................................................#.... -1 2 ...........#..............................................##.#....................##.......#.............##..............#........ -1 3 ...................#..#........#..#..##.................#......##..................#.....#....#.............#...........#...##.... -1 4 ..........#......#..................................................#...................#........#..#.....#....................... -1 5 .........#...#......#...................................#............#...#........#.............#..#........................#..... -1 6 ..##.#...........#......#.....#.#...#..............#.#......#.......#..............#.............#..................#.......#..... -1 7 ...#.................................####...........#..........#..#.....#........#...........................................#.... -1 8 ..........#......................#............................#....................#..............#......#....#.................#. -1 9 ...#.................#.........#.........#.#.....#....#.................................................................#.#....... -1 10 .......#.......................#...........#..................................#.............#..................#......#.#......... -1 11 ....................#.#.........#.........#................................#........#.#.........................#..#....#......... -1 12 ........#.............#....#.....#................................##.....#..................#.....................#............... -1 13 ..............#...................#..........................................#..........#........................#................ -1 14 ...........#...................................................................................#..........#.........##.#.......... -1 15 ............#....................#...................#.................................#.......#............#.......#............. -1 16 ..........................................#.....#.............#.#.......................##......#.....#........................... -1 17 ........................#...#..................#......................................................#.#..#...................... -1 18 ...#.....#.....................#..........................................#................................................#.....# -1 19 ..##....................................#.....................#.............#.......#............................................. -1 20 ........#..................#......#................................#.......................................................#...... -1 21 .........#................#...............................##.#.....................#...........................#...........#.#.... -1 22 ............#.....#..................................#.....#........#................#............................................ -1 23 .....................................................##..............#.....#.....#.......#........................................ -1 24 ..........#..#..............#..#...#.......................#..........................................#........................... -1 25 ...............#.............................................................#..#......#.......#..#....................#.......... -1 26 .#......................#....##.#....................#..#...#............................#............................#....#....#. -1 27 ...#...................................................................................................................#.......#.. -1 28 .#....#..............#....................#......................................#.......................#....#..#................ -1 29 ........#.....................#............................................#...............#...................................... -1 30 ....#........#..........................................................................#..............................###.......# -1 31 ........#..#.....................#..................#..................##..........#.............................................. -1 32 ..........................#...#................#....................................................#..#............#............. -1 33 ......#..............#................................#.....................#.................................#............#...... -1 34 ...................................................#....................#.................#.....#................................# -1 35 ..........................#...............#....#........................#.............................................#........... -1 36 ##..............#..............................#....#.......................#.................................................#... -1 37 ...........................#.............................................................#.#................#......#........#.##.. -1 38 ........#..................#...#..............#..............................#.....#..........#................................... -1 39 ..........#......#.##..#.....#.......##.....#......................................................#..................#........... -1 40 ....#.........#................#............................................#..#...#............#.....#..#........................ -1 41 #.......#..............#.....................#..........#....#......#......................................#........#............. -1 42 .........#..........................................................................#.........#............................#...... -1 43 ..........................#...#.......#...............#..............#.......................#...#................................ -1 44 .......................................................................#.........................................#...............# -1 45 ...................#.....#.......................................................#.............................................#.. -1 46 ...##.....................#....#................................#........#...#....#.......##...................................... -1 47 .................................................................#.......#................................#.........#............. -1 48 #..........................#.#........................#............................................#...#.........#................ -1 49 ................#.#........#...................#...............................#.........#............#.........#................. -1 50 ...#...................................#.....................................................................#..#......#.......... -1 51 .................................................#..........................................#....#................................ -1 52 ..............##.#...............................................#......................................................##........ -1 53 ...........#..............................................#.....................................##.....#.........#................ -1 54 #.......................#..........................#......#...................#.#................................................. -1 55 ..............##..........................#............................................................#................#......... -1 56 .......................#...#.........#....................................................................#............#.......... -1 57 .....#.................##.................................................#...........#..............#....#.................#..... -1 58 .........#................................................#....................#.......#.........................##.#............. -1 59 ...........#............#...#...................#.........#....................................................#.................. -1 60 ....................#.....................................................................#..........#.#.......................... -1 61 ............................##..#.......................#.......#...................#.#..........#..........................#...#. -1 62 ..................#...........#.......#.............................................................#............................. -1 63 ...##......................#....##..................................#...............#.#..#........................................ -1 64 ..#..............................##..................................................................#.......#.................... -1 65 ...#.......#..........#.#.................................................##....#...............#...............#..........#.....# -1 66 .....................................................##....................#..#..#...........................#.................... -1 67 ...#......................#........#..................#.##..............#..............#....................................#..... -1 68 ...............#..........................#..........................................................#.#..#..............#........ -1 69 ....................##....................#..#...................................#................................................ -1 70 ...........................#..............#.....................................#.........................##...................... -1 71 ...............#.................................................................#.....................#.......................... -1 72 ......#..........#...#...........#................................................#...#.................................#..#...... -1 73 ..........#............................#.............#.....................#........#............#.........#...................... -1 74 ...........#...........#..........#....#......................................................................#........#.........# -1 75 ......................#...............##.................................................................................#.....#.. -1 76 ...#................#........#..........#...................#............^...........#.................#.#......##.#.............# -1 77 .......................##..............#......#.....................................#.....#.........................#.#.#......... -1 78 .................#..#..........................................#...................#...#.........#................................ -1 79 ..#.#.....#.............................................................#.#....................................................... -1 80 ..............................#...............###...............#..........##..........................#...#..#..........##....... -1 81 .................................#.....#.........#......................................................#................#........ -1 82 ...............##.#..................................................#........................#...................#.........#..##. -1 83 .........#.................................#......#............................................................................... -1 84 ..................#......................#.#.........................................................#.....#...................... -1 85 .#..................#.............................#............................................................................... -1 86 ...................#...................#.#........#......#...........................#......................#..........#.......... -1 87 .............#...................................................................................................#................ -1 88 ..........................#................................................#........#..#.......................................... -1 89 .#........................................................#.......#......#..#..................................................... -1 90 ..................##..............#.............#................#...............................#......#......#........#....#.#.. -1 91 #.............#....................................#.....................................................#........................ -1 92 ............................#................................................................#....#.....#................#........ -1 93 .........#.......................................................................................................................# -1 94 ..#.........................................................................................#........#.#...........#.............. -1 95 ...........#......................#.......#................................#...#.......#...............#.......................... -1 96 .......................#...#..#.......#..................#........#.....#.........#............................................... -1 97 ..#..##...................#..........#................................................#.........#...................#............. -1 98 ........................##..........................#.......#..........................#...#.#.................................... -1 99 ..........................................................................................#.............#...........#............# -1 100 ..............................#.............................................................................................#.#..# -1 101 .............#..........#...........................#.........................#........#.....................#............#......# -1 102 ....................................#..........................................................................................#.. -1 103 .........#.................................#...........................................................................#.......... -1 104 .#..............................#......#......#.#................................#.................#......#....#..#..#...........# -1 105 .#...#................#.......#............#.........#...#..........#......#.........................#....................#....... -1 106 ..............#........................#................................................................................#......... -1 107 .........#........#..#.........#........#......#..#..............................................#................#............... -1 108 ...........#......................#.......#..................................#....#...#......#..........#......................... -1 109 ...........#........................................................#.#...........#................#.............................. -1 110 .#...........................#...................#...#..................................................................#........# -1 111 ....................................................................#...................#.....#......#...........................# -1 112 ...#..#..............#......................#......#..#...............#..............#..............#..............#.............. -1 113 ...........#........................#............#.#...........#.................................................................. -1 114 ............#........................#.................................................#..............#.#....................#.... -1 115 ............................#...........................#............................#........#..........................#........ -1 116 #................................................................#...........................#.............#.#........#.#.......#. -1 117 ............#..........#..............#.......#...#................................#.........#.#................#........#....#... -1 118 ...#.....................#..........#.............#....................................................#.#........................ -1 119 ...............#..................................................#......................#...............#........................ -1 120 ......#............................#..................#.....#...........................................................#...#.#... -1 121 #................#..........................................................................................#..........#.......#.. -1 122 .................................................................#..............#...................#...................#.#....... -1 123 ...............#..............................................#.................#...............#...........................#..... -1 124 .............................#...#........................#....................#........#.....................................#.## -1 125 ...............##..#.....#..#....#.#...#...........................................#.#........#........#.......#.................. -1 126 .......#........................#....#..#............................................................#.......#.................... -1 127 ........#....................#........#....#.................#........................#.............#............................. -1 128 #...#...........#......................................................#...............#...........#..#..#....#................#.. -1 129 ................................#........................................................#...#..#......#....#.....#....#...#...... -1 130 ..#..............#............................#........#....#......#.................................................#............
diff --git a/2024/06/solution.rs b/2024/06/solution.rs
@@ -0,0 +1,128 @@ -1 1 #[path = "../lib.rs"] -1 2 mod lib; -1 3 -1 4 #[derive(Copy, Clone, PartialEq)] -1 5 enum Dir { -1 6 Up, -1 7 Right, -1 8 Down, -1 9 Left, -1 10 } -1 11 -1 12 fn parse_input() -> (usize, usize, Vec<Vec<bool>>) { -1 13 let mut map = vec![]; -1 14 let mut x0 = 0; -1 15 let mut y0 = 0; -1 16 -1 17 for (y, line) in lib::iter_input().enumerate() { -1 18 let mut row = vec![]; -1 19 for (x, c) in line.chars().enumerate() { -1 20 row.push(match c { -1 21 '.' => false, -1 22 '#' => true, -1 23 _ => { -1 24 x0 = x; -1 25 y0 = y; -1 26 false -1 27 } -1 28 }); -1 29 } -1 30 map.push(row); -1 31 } -1 32 -1 33 return (x0, y0, map); -1 34 } -1 35 -1 36 fn get_next(pos: (usize, usize, Dir), map: &Vec<Vec<bool>>) -> Option<(usize, usize, Dir)> { -1 37 match pos { -1 38 (x, y, Dir::Up) => { -1 39 if y == 0 { -1 40 return None; -1 41 } else if map[y - 1][x] { -1 42 return Some((x, y, Dir::Right)); -1 43 } else { -1 44 return Some((x, y - 1, Dir::Up)); -1 45 } -1 46 } -1 47 (x, y, Dir::Right) => { -1 48 if x + 1 == map[0].len() { -1 49 return None; -1 50 } else if map[y][x + 1] { -1 51 return Some((x, y, Dir::Down)); -1 52 } else { -1 53 return Some((x + 1, y, Dir::Right)); -1 54 } -1 55 } -1 56 (x, y, Dir::Down) => { -1 57 if y + 1 == map.len() { -1 58 return None; -1 59 } else if map[y + 1][x] { -1 60 return Some((x, y, Dir::Left)); -1 61 } else { -1 62 return Some((x, y + 1, Dir::Down)); -1 63 } -1 64 } -1 65 (x, y, Dir::Left) => { -1 66 if x == 0 { -1 67 return None; -1 68 } else if map[y][x - 1] { -1 69 return Some((x, y, Dir::Up)); -1 70 } else { -1 71 return Some((x - 1, y, Dir::Left)); -1 72 } -1 73 } -1 74 }; -1 75 } -1 76 -1 77 fn check_loop(pos0: (usize, usize, Dir), map: &Vec<Vec<bool>>) -> bool { -1 78 let mut path = vec![pos0]; -1 79 let mut pos = pos0; -1 80 -1 81 loop { -1 82 if let Some(next) = get_next(pos, map) { -1 83 if path.iter().any(|v| *v == next) { -1 84 return true; -1 85 } -1 86 path.push(next); -1 87 pos = next; -1 88 } else { -1 89 return false; -1 90 } -1 91 } -1 92 } -1 93 -1 94 fn main() { -1 95 let (x0, y0, mut map) = parse_input(); -1 96 let w = map[0].len(); -1 97 let h = map.len(); -1 98 -1 99 let mut pos = (x0, y0, Dir::Up); -1 100 let mut visited = vec![vec![false; w]; h]; -1 101 let mut count1 = 0; -1 102 let mut count2 = 0; -1 103 -1 104 loop { -1 105 if !visited[pos.1][pos.0] { -1 106 count1 += 1; -1 107 visited[pos.1][pos.0] = true; -1 108 } -1 109 match get_next(pos, &map) { -1 110 Some((x2, y2, dir2)) => { -1 111 if (x2, y2) != (pos.0, pos.1) && (x2, y2) != (x0, y0) && !visited[y2][x2] { -1 112 map[y2][x2] = true; -1 113 if check_loop(pos, &map) { -1 114 count2 += 1; -1 115 } -1 116 map[y2][x2] = false; -1 117 } -1 118 pos = (x2, y2, dir2); -1 119 } -1 120 None => { -1 121 break; -1 122 } -1 123 }; -1 124 } -1 125 -1 126 println!("part1: {}", count1); -1 127 println!("part2: {}", count2); -1 128 }
diff --git a/2024/06/test.txt b/2024/06/test.txt
@@ -0,0 +1,10 @@ -1 1 ....#..... -1 2 .........# -1 3 .......... -1 4 ..#....... -1 5 .......#.. -1 6 .......... -1 7 .#..^..... -1 8 ........#. -1 9 #......... -1 10 ......#...