This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1fe7cc
commit d731f7c
Showing
5 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
...........#....................#......#.....................#.#...........#...................................................... | ||
.....................................#....#.................#...............................#..................................... | ||
..#..#.......#.............................................#.#.........................#......................#..............#.... | ||
.........................#.....#...............#.................#................................................................ | ||
.......#..........#........#.#............................................#....#.....................#..........##..#............. | ||
.........#.....................................................#..............................................................#... | ||
.....#..............................................................#................##.....#...........#.......#.........#....... | ||
.......#.........#......#...............#............................#.........#.....#.....#...#.................................. | ||
.......#...............#...................#...............#............#.......................................................#. | ||
........#.......................................................................##..#..............#...#.......................... | ||
..............#........#.......................#......#...............#..#......................#......#...#............#......... | ||
................#................#...........................................#.....#................#..........#.................. | ||
..##....................#...........#......................#..................#.......#........................................... | ||
..................................................................#..........#...................#................................ | ||
........................#.......#..........................#................................#.....................#...#.#......... | ||
...............#........#...............#.##..#.#...............#...........#......................................#.............. | ||
.#.........#.#...............................................................................#..........................#...#..... | ||
..............................#.....#.............##..............#.##....................#......#.................#....#......... | ||
..............#.............................#........#.....#..........................................#...........#............... | ||
...................#.................#..............#....##..#......................#..............................#....#.#....... | ||
.............##.....................#..........#....................................#.....................#...#.#................. | ||
.........................#..........#.......#.................................#................................................... | ||
............#.................#.................................................#.........##....................#.........#.....#. | ||
.#.........#.........................#..............#............#..#...........................##.........#...................... | ||
...........................#.....#...................#....#...........#.#.#...........#...................#....................... | ||
.................#..#......#..............................................................................#...................#... | ||
...............................#..............#...#..#......##.#.........................#........#...........#................... | ||
........#...............##.............................................................#..........................#............... | ||
.............#.............#.............#...................#...........#....................#................................... | ||
##.......................................................................................#....#...........#....#...........#...... | ||
.....................#.#.......................................#.....................................#.................#.......... | ||
......................#...................................................................#.....................#............#...# | ||
.........................#.#........................#.#................#....#....#.....#.......................................... | ||
.....................#...#.......................................#.......#.............#...................#...................... | ||
.............#...............#...............................................................................................#.... | ||
........................................................................................................#........#.........#...... | ||
.................................##....................#....#..............#.....#.....#..#....#.........................#.#.....# | ||
.........................................................................#........#........#.........................#...#........ | ||
#...................#..........#......................#..............................#.#........................#................. | ||
.#.....................................................................#..#.......#........................#...................... | ||
.......................#..............#..............#..#..............................#.......................................... | ||
...................................................................................................#...................#.......... | ||
...............#............#....................................#.....................#....................#..................... | ||
............#..#.#..........#.#.......#..........................#.................#.#..........#.......#......................... | ||
.......#....#.............................#..............................#..#........................#........#................... | ||
........#.................#.....#.#..............................#........................#...................#..........#........ | ||
............................#........................#.........................................................#.................. | ||
....................................#.............##....................................................................#..#...... | ||
..................................................#................................................#..............#........#...##. | ||
...........##.................#.........................#....................................................#...........#........ | ||
...........#.#..................#.......................#...#.......#................#.......##................................... | ||
................................................#...........#........#.......................#.....................#....#.#....... | ||
...............#........................................#............................#.....................................#...... | ||
........#...#.....#.......................................................#.#................................#.#.................. | ||
.....#............#.#...............#.#.....#....................#.#...................#...................##.........#..........# | ||
............#.................#....................................................#...........................................#.. | ||
................#..#.....#....................................#................#...#.............................................. | ||
.....................................................................#...............#............................................ | ||
..................#...........#.....#...................#....#..............................................#..................... | ||
.#............#...................................#.........#.........................................#...........#............... | ||
#.......#..#...#.....#............................................................................................................ | ||
..#.........................................................##..............................#................................#.... | ||
..............................#..............#........................................................#....................#...... | ||
#.........#.........#.............#..............#............................#..........#........................#..#............ | ||
....#...........#..........................##......#.............................#.....#........................#................. | ||
.....#.................................................................#..#....................................................... | ||
..........................#.......#.......#..................#.................................................................... | ||
................#....................#..................#......................................................................... | ||
..............................................#..........................#...........#.....#...........................#....#.#... | ||
.##...............#...#........................................................................................................... | ||
..................#..#.......................#...#..#.#.................................#.........................#...........#... | ||
..........................................................................#....................................#.................. | ||
#...........#.............................##.....................#........#....#...#........#.....................#.....#......... | ||
.......#.............................#...^....#..................................#.........#...................................... | ||
........#..............................#................................#.....#....##............................................. | ||
.................#...........................#........................#..........#..........#.................#.#..........#.....# | ||
..................................................................................................................#............... | ||
..........#.........#........................#........................................................#........................#.. | ||
................................................................................................................#................. | ||
....................#......................#....#.......#.....#................................................................... | ||
................................#........................................................#.........................###............ | ||
......#..............#.#....................#............#.......................#.......................................#........ | ||
.........................#....................................#..............#.......#............................................ | ||
..............#.................#........................................................#..#..................................... | ||
.....#.#........................#...........................................#..#.....#.................#................#......##. | ||
................#.............#................................................................................................... | ||
#....................#..........#..............##...#........#..#.........................#............#......#..................# | ||
...........#..#.......#.....................#..........................................................#........#................. | ||
...............................................#................#...........#................................#.................... | ||
.............#.......#.................#........#.............................................#...........#....................... | ||
....................#.......#.....#.......................#.................#.............#.........................#............. | ||
.#..............#................................................................................................................. | ||
..#.#......................................#.................#.........#...................................#.#.................... | ||
......#..............................#........#.#..................#................#.................................#........... | ||
..............#........................................#.......................................................................... | ||
.#...........#.........#...........#...................#................#..................................#............#..#...... | ||
.........#....#...................#..#.................................................................#....#..................... | ||
..........#...........................#.....................................#...............#.......#...#......................... | ||
...........#..................................#............................................................##................#.... | ||
.......................#..#....................................................................................................... | ||
...............................................#......................#.#.#.............#..#........#...........................#. | ||
..#.................................................#..............................#.....................................#........ | ||
..........#..#..........#...................#......................................##......#.......#........#.....#............... | ||
.#.#...............................................##...#................#..............#.....#................................... | ||
....................#..#...............................#..#.......#....................#....#.....................#.....#.#....... | ||
#...................#...........#...................................................#...........................#..##.........###. | ||
.............#.........#......#..........................#.................#......##................#...............#...#......... | ||
........#.....................................................................................#................................... | ||
..#...........#...................................#...................#............................#.........#.................#.. | ||
.................................................................................#................................................ | ||
.#........#.................#......................................#.....#...........#................#........................... | ||
#...........................#..............................#....#............................................................#.... | ||
#..........#.............................................#..........#.......................................................#..... | ||
.........#...#...#...#............................................................#....#..........#.......................#....... | ||
.........................#.........................#........#............#..............#.....##.........................#...#.... | ||
...............................#...........#..............................................#......................#......#......... | ||
......................#........................#..#.....................#...............................#........................# | ||
......#...##..............................................................................................................#....... | ||
............#.............#.#...............................................#.........#..........................#...........#.... | ||
.......................................#............#...............................................#............................. | ||
....#........#.............................#...............#.#................................................#................... | ||
.........#.#........................#.....#...##...#.........###............#.#.........#......#.........#................#.....#. | ||
...........#....................#......................##...............#.......................................................#. | ||
#......................................................#.................##....................................#........#.....#... | ||
..#..........................##.................#...........................................#..................................... | ||
.......................................#............#......#...#.........#......##......................................#......... | ||
.#.......#.........................................##..............................................#.#...................#........ | ||
.................#.........#..........................##............................#..#.....#.................................... | ||
.....#....................................#......................................#.......................#...........#..#..#...... | ||
.......#......#.........#.....#........#......#.................#...........#............#..................#....#...........#.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4939 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1434 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package adventofcode.solutions | ||
|
||
import adventofcode.Definitions.* | ||
|
||
@main def Day06 = Day(6) { (input, part) => | ||
|
||
case class Vec(i: Int, j: Int): | ||
infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j) | ||
def turnRight: Vec = Vec(j, -i) | ||
|
||
val (grid, initialPosition) = | ||
val matrix = input.toLines | ||
matrix.map(_.map { | ||
case '#' => false | ||
case _ => true | ||
}) -> matrix.indices.flatMap(i => matrix(i).indices.collect { case j if matrix(i)(j) == '^' => Vec(i, j) }).head | ||
val initialDirection = Vec(-1, 0) | ||
|
||
def inBounds(vec: Vec): Boolean = grid.indices.contains(vec.i) && grid(vec.i).indices.contains(vec.j) | ||
|
||
def visit(position: Vec, direction: Vec, history: Set[(Vec, Vec)], grid: IndexedSeq[IndexedSeq[Boolean]]): Option[Set[(Vec, Vec)]] = | ||
val key = (position, direction) | ||
if history.contains(key) then | ||
None | ||
else | ||
val nextHistory = history + key | ||
val nextCandidatePosition = position + direction | ||
if inBounds(nextCandidatePosition) then | ||
if grid(nextCandidatePosition.i)(nextCandidatePosition.j) then | ||
visit(nextCandidatePosition, direction, nextHistory, grid) | ||
else | ||
visit(position, direction.turnRight, nextHistory, grid) | ||
else | ||
Some(nextHistory) | ||
|
||
val explored = visit(initialPosition, initialDirection, Set.empty, grid).get | ||
|
||
part(1) = explored.map((p, _) => p).size | ||
|
||
val candidates = explored.map(_ + _).filter(inBounds).filter(p => grid(p.i)(p.j)) | ||
|
||
part(2) = candidates.count(p => visit(initialPosition, initialDirection, Set.empty, grid.updated(p.i, grid(p.i).updated(p.j, false))).isEmpty) | ||
|
||
} |