From dc54e2d7765a0426a96d8b622dd0dc64876797ab Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 8 Dec 2024 13:32:39 +0100 Subject: [PATCH] Day 6 part 1 --- 2024/src/main/kotlin/Grid.kt | 15 ++++ 2024/src/main/kotlin/day6.kt | 44 +++++++++++ 2024/src/main/resources/day6.txt | 130 +++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 2024/src/main/kotlin/day6.kt create mode 100644 2024/src/main/resources/day6.txt diff --git a/2024/src/main/kotlin/Grid.kt b/2024/src/main/kotlin/Grid.kt index cfad170..dac44f2 100644 --- a/2024/src/main/kotlin/Grid.kt +++ b/2024/src/main/kotlin/Grid.kt @@ -61,4 +61,19 @@ class Grid(val rows: List) { 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.. 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() + + 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()}") +} \ No newline at end of file diff --git a/2024/src/main/resources/day6.txt b/2024/src/main/resources/day6.txt new file mode 100644 index 0000000..97429eb --- /dev/null +++ b/2024/src/main/resources/day6.txt @@ -0,0 +1,130 @@ +....#.......#.............#..##...................#..#..#..................................#....#................................. +................................#..#.............##.....#....#....................#.............................................#. +.#....#.........................................................................................#.#............................... +.......#.......#..................#........#..................................#...............#...........#....................... +..#......................#.....#...........#..........................................................................#........... +.#.............#.............#....#...............#..#................................................#..........#................ +.#............................................#........#........................................#...............................#. +...............#...........#....#............................................#.......#...................................#........ +.#.........................#................#.........#..##.....#........................................#........................ +.........#...#......#..................#...........................................................#...#..............#........... +..................#..#....................#...................#.....................#......#....#...........#..#.........#........ +.........................................................................................#..........#............................. +...........#...........#.....#..................#....................#............................................................ +........#...........................................#............#...........................#.........................#.......... +....##....##..............................#.................................................................#....#....#.......#... +..#............#.........##....................................................#.....................#.....................#...... +......#.......#.##.......##...........................................#........#................#................................. +..#...#.........................................#....................#.....#........#....#......#......#................#......... +.#...................##............................................................#....................#.......................#. +......#....#.....#............#............#.................................................#..#.......#..........#.............. +...............#....#.........#.............................................................................................#..... +...........#..........................#..............................................#.........#...............#..#..............# +...........#...#.................#........................................#...................#....#.............................. +........#................................#...........................................................#.......#.................... +...........#....................#.....#.........................................................#..............#....#...#......... +..........#........................................................#..............#..............................#................ +..............................##...........................................#.....................................................# +...#..#........................................................................................................#........#.....##.. +.......#........#........#.#.....................#................................................#.......................#...#... +...............#.......................................................................#.........#......#.#.##.................... +......#..................#.....#..................#..#..................#.................#..#.....#...................#....#..... +.....#........#.............#....#...............................#.................#.............................................. +....................................................#.......#......................................#....#......................... +...#.............#...............#.............#......#.#..............#......#.............#..................................... +..............#..................#............................................#..#.........................#...................... +.............#............#........#........................................#..................................................... +...#.........#.............#.........#............#..........................................#................#...#............... +...........#.................#...................................................................#................................ +...#..............................................................................................................#............... +..........#...............#...#......#...............##..............................................#....#...................#... +.........................................#...................................................#..#.....................#........... +......#..#.........#....................................#....................#..........#......#.................................. +........#.........................................#................#.#.......#.......................#............#..............# +..........................#...............#....................#.............................#..............#..................... +..........................#...........#............................................#....#...#..................................... +...........#........#..........#..............................................................#...##......#.......#......#........ +....#..........#...#...................##..........#.........................................#....#............................... +...............................................................#................................................#..........#...... +................................................................................................................#.....#........... +...#.....................#......................................................................#....................#............ +......................##...........#.......#.......#.......................................#...................................... +......#......#........#....................................................................................#...................... +.......#......................#.........................................................#.......#................................. +...............#..........#...#.#..........#.....#..............##.......#.....#....................................#............. +.......##....................................................................................#.#...#..........................#... +......#.............#.#.........................#....#......#......#...............#.........#..#................................. +.........................#........#.........................#....#..................#.................#............#......#.#..... +............#............#......................#.#................##........#.................................................... +......#..............................#...........#.......#........#.................#............................................. +............................................#.....................................................#.................#...#......... +.................#.......................................#..................##...#..............................#......#.......#.. +.#..#................#.#......................................#..........#..........#...........................#.......#......... +.........................................................................#.....##.............................#.............#..... +.#.......................#.....................#....##....#........................................#.............................. +......#...........................................##..........#......#.........................#..#..........#....#...#........... +........#.#....................................................................................#.................................. +..........#..............................#.....#......#.........#............................#........................#........... +....................#................................................................#...............................#........#... +....................................#.......#...................................##.........................#...................... +.....................#.....................................#.........#..........................#................................. +.......#.................................#.....................#........................................#....................#.... +......................................................................#.....#........................#.......................#.... +..#............................#..................#...#.........#..........#..#.........................................#.#....... +....#............................................................................#.............................#..#.....#......... +......#..................................#.............................................................................#.........# +....#......................................................................#................#.....#...........................#... +.............#.........................#.#....................#........................#......................................#... +....#......................................................................#...#..................#..........#....#............... +.......#....#.#..#..#....#........#.....................................................#..................#..#................... +#..............#....................................#.......................................#.....................#...........#... +#.........#....#..#.........#.................#..................#.#..............................##......................#....... +................#.........#......................................................................................................# +...............................#.........#.................................................................#.....#.#.............. +..........#................#.............................#.............#.......#..........................................#....... +.....#.........#........#..............#.......................................#.........^........................................ +........................#................#........#.......................................#............................#...#...... +......#.....#.......#.................................................#...............#.....#..................................... +..................................................#......#.....................................#....#....#.##....................# +.........................................................................#...#..............#...............#..................... +..........#.................#...................#.........................................................................#....#.. +...........#.....................#.....#.......................................#....................#............#........#....... +......................#...........#........................................................................................#...... +..........#.......#.................................................................................#.......#..................... +........#.....................................#..................................................##............................... +.......#................................#.........#..#..................................................................#......... +...........#...................##.............#...............................#..........#....................#.....#...#......... +....................................##..#..........................................#.............................................. +...............#..#............#..............................#.#.#........#......#............................................... +..#..................................#....#....................................................................................... +........................#.................##.........#......................#........................#.#.#........................ +.......................................#.##....................................................................#.................. +..................................##............#..............................................................................#.. +..#.................................................#.....................................................................#....... +................#..#..#...#.........#...#.................................................................#.....................## +.....#......................#...........#.............#........................................................................... +........#.....................#..............................................#................#..............................#.... +........#.............#.........................#.............................................#................#..........#......# +#........#...................................#....#......#...............................#........................................ +...........#........#..................................................#.............#..........................#.............#... +....................#..#.#.................................................#.#...........#................................#.#..... +.......................#...................................................#....................................................#. +......#......................................#..........##.......................#...........#.........#.......................... +.......#..................#..................#......................#............................#.............#...#.............. +.....................................................................................#........#................#.........#........ +............................#..............................................................#.....#........#......#................ +..#..........................#.........#.................#...........#...#....#........................#.......#.............#.... +.#.....#.....#.....................#........#...............#............#.##........#..................................#......... +............#.#..................................#..#.........#.................#............................#........#.....#..... +............#.........................#..................................#........................................................ +....................#................#...........#..........................................................................#..... +....................#.....................................................#.........#......#...........#...#.#..................#. +........#..........#.......................................#...............#....#............#.....................#.............. +.......#...#......#...................#.......#..........................#.........................#...................#.......... +.#............#....###...............###................................#.............................#........................... +.............................#.......#..............................#.#.........................................#..#.............. +...............#.........#...................#...................................#..#..................#.......................... +........#.......#..#..................#........................................................................#...............#.. +.#.............#.....................#........#..........................#.........#..........................#...........#....... +.....#.............#.....#..........#.....#.............#............#.............................#..........................#... +...................#.......................#......#.#..............#..###.....#......................#...........#.......#.#...... \ No newline at end of file