Skip to content
This repository was archived by the owner on Jan 5, 2025. It is now read-only.

Commit d731f7c

Browse files
Solved day 06
1 parent c1fe7cc commit d731f7c

File tree

5 files changed

+177
-1
lines changed

5 files changed

+177
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _My solutions to the 2024 edition of [Advent of Code](https://adventofcode.com/2
2121
| **[03](https://adventofcode.com/2024/day/3)** | [solution](src/main/scala/adventofcode/solutions/Day03.scala) |
2222
| **[04](https://adventofcode.com/2024/day/4)** | [solution](src/main/scala/adventofcode/solutions/Day04.scala) |
2323
| **[05](https://adventofcode.com/2024/day/5)** | [solution](src/main/scala/adventofcode/solutions/Day05.scala) |
24-
| **[06](https://adventofcode.com/2024/day/6)** | [](src/main/scala/adventofcode/solutions/Day06.scala) |
24+
| **[06](https://adventofcode.com/2024/day/6)** | [solution](src/main/scala/adventofcode/solutions/Day06.scala) |
2525
| **[07](https://adventofcode.com/2024/day/7)** | [](src/main/scala/adventofcode/solutions/Day07.scala) |
2626
| **[08](https://adventofcode.com/2024/day/8)** | [](src/main/scala/adventofcode/solutions/Day08.scala) |
2727
| **[09](https://adventofcode.com/2024/day/9)** | [](src/main/scala/adventofcode/solutions/Day09.scala) |

input/06.txt

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
...........#....................#......#.....................#.#...........#......................................................
2+
.....................................#....#.................#...............................#.....................................
3+
..#..#.......#.............................................#.#.........................#......................#..............#....
4+
.........................#.....#...............#.................#................................................................
5+
.......#..........#........#.#............................................#....#.....................#..........##..#.............
6+
.........#.....................................................#..............................................................#...
7+
.....#..............................................................#................##.....#...........#.......#.........#.......
8+
.......#.........#......#...............#............................#.........#.....#.....#...#..................................
9+
.......#...............#...................#...............#............#.......................................................#.
10+
........#.......................................................................##..#..............#...#..........................
11+
..............#........#.......................#......#...............#..#......................#......#...#............#.........
12+
................#................#...........................................#.....#................#..........#..................
13+
..##....................#...........#......................#..................#.......#...........................................
14+
..................................................................#..........#...................#................................
15+
........................#.......#..........................#................................#.....................#...#.#.........
16+
...............#........#...............#.##..#.#...............#...........#......................................#..............
17+
.#.........#.#...............................................................................#..........................#...#.....
18+
..............................#.....#.............##..............#.##....................#......#.................#....#.........
19+
..............#.............................#........#.....#..........................................#...........#...............
20+
...................#.................#..............#....##..#......................#..............................#....#.#.......
21+
.............##.....................#..........#....................................#.....................#...#.#.................
22+
.........................#..........#.......#.................................#...................................................
23+
............#.................#.................................................#.........##....................#.........#.....#.
24+
.#.........#.........................#..............#............#..#...........................##.........#......................
25+
...........................#.....#...................#....#...........#.#.#...........#...................#.......................
26+
.................#..#......#..............................................................................#...................#...
27+
...............................#..............#...#..#......##.#.........................#........#...........#...................
28+
........#...............##.............................................................#..........................#...............
29+
.............#.............#.............#...................#...........#....................#...................................
30+
##.......................................................................................#....#...........#....#...........#......
31+
.....................#.#.......................................#.....................................#.................#..........
32+
......................#...................................................................#.....................#............#...#
33+
.........................#.#........................#.#................#....#....#.....#..........................................
34+
.....................#...#.......................................#.......#.............#...................#......................
35+
.............#...............#...............................................................................................#....
36+
........................................................................................................#........#.........#......
37+
.................................##....................#....#..............#.....#.....#..#....#.........................#.#.....#
38+
.........................................................................#........#........#.........................#...#........
39+
#...................#..........#......................#..............................#.#........................#.................
40+
.#.....................................................................#..#.......#........................#......................
41+
.......................#..............#..............#..#..............................#..........................................
42+
...................................................................................................#...................#..........
43+
...............#............#....................................#.....................#....................#.....................
44+
............#..#.#..........#.#.......#..........................#.................#.#..........#.......#.........................
45+
.......#....#.............................#..............................#..#........................#........#...................
46+
........#.................#.....#.#..............................#........................#...................#..........#........
47+
............................#........................#.........................................................#..................
48+
....................................#.............##....................................................................#..#......
49+
..................................................#................................................#..............#........#...##.
50+
...........##.................#.........................#....................................................#...........#........
51+
...........#.#..................#.......................#...#.......#................#.......##...................................
52+
................................................#...........#........#.......................#.....................#....#.#.......
53+
...............#........................................#............................#.....................................#......
54+
........#...#.....#.......................................................#.#................................#.#..................
55+
.....#............#.#...............#.#.....#....................#.#...................#...................##.........#..........#
56+
............#.................#....................................................#...........................................#..
57+
................#..#.....#....................................#................#...#..............................................
58+
.....................................................................#...............#............................................
59+
..................#...........#.....#...................#....#..............................................#.....................
60+
.#............#...................................#.........#.........................................#...........#...............
61+
#.......#..#...#.....#............................................................................................................
62+
..#.........................................................##..............................#................................#....
63+
..............................#..............#........................................................#....................#......
64+
#.........#.........#.............#..............#............................#..........#........................#..#............
65+
....#...........#..........................##......#.............................#.....#........................#.................
66+
.....#.................................................................#..#.......................................................
67+
..........................#.......#.......#..................#....................................................................
68+
................#....................#..................#.........................................................................
69+
..............................................#..........................#...........#.....#...........................#....#.#...
70+
.##...............#...#...........................................................................................................
71+
..................#..#.......................#...#..#.#.................................#.........................#...........#...
72+
..........................................................................#....................................#..................
73+
#...........#.............................##.....................#........#....#...#........#.....................#.....#.........
74+
.......#.............................#...^....#..................................#.........#......................................
75+
........#..............................#................................#.....#....##.............................................
76+
.................#...........................#........................#..........#..........#.................#.#..........#.....#
77+
..................................................................................................................#...............
78+
..........#.........#........................#........................................................#........................#..
79+
................................................................................................................#.................
80+
....................#......................#....#.......#.....#...................................................................
81+
................................#........................................................#.........................###............
82+
......#..............#.#....................#............#.......................#.......................................#........
83+
.........................#....................................#..............#.......#............................................
84+
..............#.................#........................................................#..#.....................................
85+
.....#.#........................#...........................................#..#.....#.................#................#......##.
86+
................#.............#...................................................................................................
87+
#....................#..........#..............##...#........#..#.........................#............#......#..................#
88+
...........#..#.......#.....................#..........................................................#........#.................
89+
...............................................#................#...........#................................#....................
90+
.............#.......#.................#........#.............................................#...........#.......................
91+
....................#.......#.....#.......................#.................#.............#.........................#.............
92+
.#..............#.................................................................................................................
93+
..#.#......................................#.................#.........#...................................#.#....................
94+
......#..............................#........#.#..................#................#.................................#...........
95+
..............#........................................#..........................................................................
96+
.#...........#.........#...........#...................#................#..................................#............#..#......
97+
.........#....#...................#..#.................................................................#....#.....................
98+
..........#...........................#.....................................#...............#.......#...#.........................
99+
...........#..................................#............................................................##................#....
100+
.......................#..#.......................................................................................................
101+
...............................................#......................#.#.#.............#..#........#...........................#.
102+
..#.................................................#..............................#.....................................#........
103+
..........#..#..........#...................#......................................##......#.......#........#.....#...............
104+
.#.#...............................................##...#................#..............#.....#...................................
105+
....................#..#...............................#..#.......#....................#....#.....................#.....#.#.......
106+
#...................#...........#...................................................#...........................#..##.........###.
107+
.............#.........#......#..........................#.................#......##................#...............#...#.........
108+
........#.....................................................................................#...................................
109+
..#...........#...................................#...................#............................#.........#.................#..
110+
.................................................................................#................................................
111+
.#........#.................#......................................#.....#...........#................#...........................
112+
#...........................#..............................#....#............................................................#....
113+
#..........#.............................................#..........#.......................................................#.....
114+
.........#...#...#...#............................................................#....#..........#.......................#.......
115+
.........................#.........................#........#............#..............#.....##.........................#...#....
116+
...............................#...........#..............................................#......................#......#.........
117+
......................#........................#..#.....................#...............................#........................#
118+
......#...##..............................................................................................................#.......
119+
............#.............#.#...............................................#.........#..........................#...........#....
120+
.......................................#............#...............................................#.............................
121+
....#........#.............................#...............#.#................................................#...................
122+
.........#.#........................#.....#...##...#.........###............#.#.........#......#.........#................#.....#.
123+
...........#....................#......................##...............#.......................................................#.
124+
#......................................................#.................##....................................#........#.....#...
125+
..#..........................##.................#...........................................#.....................................
126+
.......................................#............#......#...#.........#......##......................................#.........
127+
.#.......#.........................................##..............................................#.#...................#........
128+
.................#.........#..........................##............................#..#.....#....................................
129+
.....#....................................#......................................#.......................#...........#..#..#......
130+
.......#......#.........#.....#........#......#.................#...........#............#..................#....#...........#....

output/06-1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4939

output/06-2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1434
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package adventofcode.solutions
2+
3+
import adventofcode.Definitions.*
4+
5+
@main def Day06 = Day(6) { (input, part) =>
6+
7+
case class Vec(i: Int, j: Int):
8+
infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j)
9+
def turnRight: Vec = Vec(j, -i)
10+
11+
val (grid, initialPosition) =
12+
val matrix = input.toLines
13+
matrix.map(_.map {
14+
case '#' => false
15+
case _ => true
16+
}) -> matrix.indices.flatMap(i => matrix(i).indices.collect { case j if matrix(i)(j) == '^' => Vec(i, j) }).head
17+
val initialDirection = Vec(-1, 0)
18+
19+
def inBounds(vec: Vec): Boolean = grid.indices.contains(vec.i) && grid(vec.i).indices.contains(vec.j)
20+
21+
def visit(position: Vec, direction: Vec, history: Set[(Vec, Vec)], grid: IndexedSeq[IndexedSeq[Boolean]]): Option[Set[(Vec, Vec)]] =
22+
val key = (position, direction)
23+
if history.contains(key) then
24+
None
25+
else
26+
val nextHistory = history + key
27+
val nextCandidatePosition = position + direction
28+
if inBounds(nextCandidatePosition) then
29+
if grid(nextCandidatePosition.i)(nextCandidatePosition.j) then
30+
visit(nextCandidatePosition, direction, nextHistory, grid)
31+
else
32+
visit(position, direction.turnRight, nextHistory, grid)
33+
else
34+
Some(nextHistory)
35+
36+
val explored = visit(initialPosition, initialDirection, Set.empty, grid).get
37+
38+
part(1) = explored.map((p, _) => p).size
39+
40+
val candidates = explored.map(_ + _).filter(inBounds).filter(p => grid(p.i)(p.j))
41+
42+
part(2) = candidates.count(p => visit(initialPosition, initialDirection, Set.empty, grid.updated(p.i, grid(p.i).updated(p.j, false))).isEmpty)
43+
44+
}

0 commit comments

Comments
 (0)