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

Commit

Permalink
Solved day 06
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianCassayre committed Dec 6, 2024
1 parent c1fe7cc commit d731f7c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _My solutions to the 2024 edition of [Advent of Code](https://adventofcode.com/2
| **[03](https://adventofcode.com/2024/day/3)** | [solution](src/main/scala/adventofcode/solutions/Day03.scala) |
| **[04](https://adventofcode.com/2024/day/4)** | [solution](src/main/scala/adventofcode/solutions/Day04.scala) |
| **[05](https://adventofcode.com/2024/day/5)** | [solution](src/main/scala/adventofcode/solutions/Day05.scala) |
| **[06](https://adventofcode.com/2024/day/6)** | [](src/main/scala/adventofcode/solutions/Day06.scala) |
| **[06](https://adventofcode.com/2024/day/6)** | [solution](src/main/scala/adventofcode/solutions/Day06.scala) |
| **[07](https://adventofcode.com/2024/day/7)** | [](src/main/scala/adventofcode/solutions/Day07.scala) |
| **[08](https://adventofcode.com/2024/day/8)** | [](src/main/scala/adventofcode/solutions/Day08.scala) |
| **[09](https://adventofcode.com/2024/day/9)** | [](src/main/scala/adventofcode/solutions/Day09.scala) |
Expand Down
130 changes: 130 additions & 0 deletions input/06.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
...........#....................#......#.....................#.#...........#......................................................
.....................................#....#.................#...............................#.....................................
..#..#.......#.............................................#.#.........................#......................#..............#....
.........................#.....#...............#.................#................................................................
.......#..........#........#.#............................................#....#.....................#..........##..#.............
.........#.....................................................#..............................................................#...
.....#..............................................................#................##.....#...........#.......#.........#.......
.......#.........#......#...............#............................#.........#.....#.....#...#..................................
.......#...............#...................#...............#............#.......................................................#.
........#.......................................................................##..#..............#...#..........................
..............#........#.......................#......#...............#..#......................#......#...#............#.........
................#................#...........................................#.....#................#..........#..................
..##....................#...........#......................#..................#.......#...........................................
..................................................................#..........#...................#................................
........................#.......#..........................#................................#.....................#...#.#.........
...............#........#...............#.##..#.#...............#...........#......................................#..............
.#.........#.#...............................................................................#..........................#...#.....
..............................#.....#.............##..............#.##....................#......#.................#....#.........
..............#.............................#........#.....#..........................................#...........#...............
...................#.................#..............#....##..#......................#..............................#....#.#.......
.............##.....................#..........#....................................#.....................#...#.#.................
.........................#..........#.......#.................................#...................................................
............#.................#.................................................#.........##....................#.........#.....#.
.#.........#.........................#..............#............#..#...........................##.........#......................
...........................#.....#...................#....#...........#.#.#...........#...................#.......................
.................#..#......#..............................................................................#...................#...
...............................#..............#...#..#......##.#.........................#........#...........#...................
........#...............##.............................................................#..........................#...............
.............#.............#.............#...................#...........#....................#...................................
##.......................................................................................#....#...........#....#...........#......
.....................#.#.......................................#.....................................#.................#..........
......................#...................................................................#.....................#............#...#
.........................#.#........................#.#................#....#....#.....#..........................................
.....................#...#.......................................#.......#.............#...................#......................
.............#...............#...............................................................................................#....
........................................................................................................#........#.........#......
.................................##....................#....#..............#.....#.....#..#....#.........................#.#.....#
.........................................................................#........#........#.........................#...#........
#...................#..........#......................#..............................#.#........................#.................
.#.....................................................................#..#.......#........................#......................
.......................#..............#..............#..#..............................#..........................................
...................................................................................................#...................#..........
...............#............#....................................#.....................#....................#.....................
............#..#.#..........#.#.......#..........................#.................#.#..........#.......#.........................
.......#....#.............................#..............................#..#........................#........#...................
........#.................#.....#.#..............................#........................#...................#..........#........
............................#........................#.........................................................#..................
....................................#.............##....................................................................#..#......
..................................................#................................................#..............#........#...##.
...........##.................#.........................#....................................................#...........#........
...........#.#..................#.......................#...#.......#................#.......##...................................
................................................#...........#........#.......................#.....................#....#.#.......
...............#........................................#............................#.....................................#......
........#...#.....#.......................................................#.#................................#.#..................
.....#............#.#...............#.#.....#....................#.#...................#...................##.........#..........#
............#.................#....................................................#...........................................#..
................#..#.....#....................................#................#...#..............................................
.....................................................................#...............#............................................
..................#...........#.....#...................#....#..............................................#.....................
.#............#...................................#.........#.........................................#...........#...............
#.......#..#...#.....#............................................................................................................
..#.........................................................##..............................#................................#....
..............................#..............#........................................................#....................#......
#.........#.........#.............#..............#............................#..........#........................#..#............
....#...........#..........................##......#.............................#.....#........................#.................
.....#.................................................................#..#.......................................................
..........................#.......#.......#..................#....................................................................
................#....................#..................#.........................................................................
..............................................#..........................#...........#.....#...........................#....#.#...
.##...............#...#...........................................................................................................
..................#..#.......................#...#..#.#.................................#.........................#...........#...
..........................................................................#....................................#..................
#...........#.............................##.....................#........#....#...#........#.....................#.....#.........
.......#.............................#...^....#..................................#.........#......................................
........#..............................#................................#.....#....##.............................................
.................#...........................#........................#..........#..........#.................#.#..........#.....#
..................................................................................................................#...............
..........#.........#........................#........................................................#........................#..
................................................................................................................#.................
....................#......................#....#.......#.....#...................................................................
................................#........................................................#.........................###............
......#..............#.#....................#............#.......................#.......................................#........
.........................#....................................#..............#.......#............................................
..............#.................#........................................................#..#.....................................
.....#.#........................#...........................................#..#.....#.................#................#......##.
................#.............#...................................................................................................
#....................#..........#..............##...#........#..#.........................#............#......#..................#
...........#..#.......#.....................#..........................................................#........#.................
...............................................#................#...........#................................#....................
.............#.......#.................#........#.............................................#...........#.......................
....................#.......#.....#.......................#.................#.............#.........................#.............
.#..............#.................................................................................................................
..#.#......................................#.................#.........#...................................#.#....................
......#..............................#........#.#..................#................#.................................#...........
..............#........................................#..........................................................................
.#...........#.........#...........#...................#................#..................................#............#..#......
.........#....#...................#..#.................................................................#....#.....................
..........#...........................#.....................................#...............#.......#...#.........................
...........#..................................#............................................................##................#....
.......................#..#.......................................................................................................
...............................................#......................#.#.#.............#..#........#...........................#.
..#.................................................#..............................#.....................................#........
..........#..#..........#...................#......................................##......#.......#........#.....#...............
.#.#...............................................##...#................#..............#.....#...................................
....................#..#...............................#..#.......#....................#....#.....................#.....#.#.......
#...................#...........#...................................................#...........................#..##.........###.
.............#.........#......#..........................#.................#......##................#...............#...#.........
........#.....................................................................................#...................................
..#...........#...................................#...................#............................#.........#.................#..
.................................................................................#................................................
.#........#.................#......................................#.....#...........#................#...........................
#...........................#..............................#....#............................................................#....
#..........#.............................................#..........#.......................................................#.....
.........#...#...#...#............................................................#....#..........#.......................#.......
.........................#.........................#........#............#..............#.....##.........................#...#....
...............................#...........#..............................................#......................#......#.........
......................#........................#..#.....................#...............................#........................#
......#...##..............................................................................................................#.......
............#.............#.#...............................................#.........#..........................#...........#....
.......................................#............#...............................................#.............................
....#........#.............................#...............#.#................................................#...................
.........#.#........................#.....#...##...#.........###............#.#.........#......#.........#................#.....#.
...........#....................#......................##...............#.......................................................#.
#......................................................#.................##....................................#........#.....#...
..#..........................##.................#...........................................#.....................................
.......................................#............#......#...#.........#......##......................................#.........
.#.......#.........................................##..............................................#.#...................#........
.................#.........#..........................##............................#..#.....#....................................
.....#....................................#......................................#.......................#...........#..#..#......
.......#......#.........#.....#........#......#.................#...........#............#..................#....#...........#....
1 change: 1 addition & 0 deletions output/06-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4939
1 change: 1 addition & 0 deletions output/06-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1434
44 changes: 44 additions & 0 deletions src/main/scala/adventofcode/solutions/Day06.scala
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)

}

0 comments on commit d731f7c

Please sign in to comment.