Day 6 part 1

This commit is contained in:
Sven Weidauer 2024-12-08 13:32:39 +01:00
parent 5de9f74342
commit dc54e2d776
3 changed files with 189 additions and 0 deletions

View file

@ -61,4 +61,19 @@ class Grid(val rows: List<String>) {
fun get(x: Int, y: Int): Char = rows[y][x] fun get(x: Int, y: Int): Char = rows[y][x]
fun find(char: Char): Coordinate? {
for (y in rows.indices) {
val index = rows[y].indexOf(char)
if (index != -1) {
return Coordinate(index, y)
}
}
return null
}
fun inside(coordinate: Coordinate) = coordinate.x in 0..<width && coordinate.y in 0..<height
operator fun get(coordinate: Coordinate) = get(coordinate.x, coordinate.y)
data class Coordinate(val x: Int, val y: Int)
} }

View file

@ -0,0 +1,44 @@
enum class Direction {
North,
East,
South,
West;
fun turnClockwise() = when(this) {
North -> East
East -> South
South -> West
West -> North
}
}
fun Grid.Coordinate.step(direction: Direction) =
when (direction) {
Direction.North -> copy(y = y - 1)
Direction.East -> copy(x = x + 1)
Direction.South -> copy(y = y + 1)
Direction.West -> copy(x = x - 1)
}
fun main() {
val grid = Grid.read("day6.txt")
var position = grid.find('^') ?: error("Guard not found")
var direction = Direction.North
val visitedPositions = mutableSetOf<Grid.Coordinate>()
while (true) {
visitedPositions.add(position)
val next = position.step(direction)
if (!grid.inside(next)) break
if (grid[next] == '#') {
direction = direction.turnClockwise()
} else {
position = next
}
}
println("Part 1: ${visitedPositions.count()}")
}

View file

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