|
| 1 | +enum class Direction(val x: Int, val y: Int) { |
| 2 | + UP(0, -1), |
| 3 | + RIGHT(1, 0), |
| 4 | + DOWN(0, 1), |
| 5 | + LEFT(-1, 0); |
| 6 | + |
| 7 | + fun rotate() = entries[(ordinal + 1) % entries.size] |
| 8 | +} |
| 9 | + |
| 10 | +sealed class Field { |
| 11 | + data object Guard : Field() |
| 12 | + data object Wall : Field() |
| 13 | + data object NotVisited : Field() |
| 14 | + data object Visited : Field() |
| 15 | + data object NotExists : Field() |
| 16 | +} |
| 17 | + |
| 18 | +data class Array2d(val xSize: Int, val ySize: Int) { |
| 19 | + |
| 20 | + val array = Array(ySize) { Array<Field>(xSize) { Field.NotVisited } } |
| 21 | + |
| 22 | + operator fun get(x: Int, y: Int) = if (isInBounds(x, y)) array[y][x] else Field.NotExists |
| 23 | + operator fun set(x: Int, y: Int, field: Field) { |
| 24 | + array[y][x] = field |
| 25 | + } |
| 26 | + |
| 27 | + fun isInBounds(x: Int, y: Int) = x in 0 until xSize && y in 0 until ySize |
| 28 | + |
| 29 | + fun count(field: Field) = array.sumOf { it.count { it == field } } |
| 30 | + |
| 31 | + fun printArray() { |
| 32 | + for (x in array.indices) { |
| 33 | + for (y in array[x].indices) { |
| 34 | + print( |
| 35 | + when (array[x][y]) { |
| 36 | + Field.Guard -> '^' |
| 37 | + Field.Wall -> '#' |
| 38 | + Field.NotVisited -> '.' |
| 39 | + Field.Visited -> 'X' |
| 40 | + else -> ' ' |
| 41 | + } |
| 42 | + ) |
| 43 | + } |
| 44 | + println("") |
| 45 | + } |
| 46 | + println("") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +data class Point(val x: Int, val y: Int) |
| 51 | + |
| 52 | +fun main() { |
| 53 | + |
| 54 | + fun part1(input: List<String>): Int { |
| 55 | + |
| 56 | + val array = Array2d(input[0].length, input.size) |
| 57 | + var guardPos = Point(0, 0) |
| 58 | + var direction = Direction.UP |
| 59 | + |
| 60 | + input.forEachIndexed { y, list -> |
| 61 | + list.forEachIndexed { x, char -> |
| 62 | + array.set( |
| 63 | + x = x, |
| 64 | + y = y, |
| 65 | + field = when (char) { |
| 66 | + '#' -> Field.Wall |
| 67 | + '.' -> Field.NotVisited |
| 68 | + '^' -> { |
| 69 | + guardPos = Point(x, y) |
| 70 | + Field.Visited |
| 71 | + } |
| 72 | + |
| 73 | + else -> error("Unknown char: $char") |
| 74 | + } |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + do { |
| 80 | + // array.printArray() |
| 81 | + while (array[guardPos.x + direction.x, guardPos.y + direction.y] == Field.Wall) { |
| 82 | + direction = direction.rotate() |
| 83 | + } |
| 84 | + array[guardPos.x, guardPos.y] = Field.Visited |
| 85 | + guardPos = Point(guardPos.x + direction.x, guardPos.y + direction.y) |
| 86 | + |
| 87 | + |
| 88 | + } while (array[guardPos.x, guardPos.y] != Field.NotExists) |
| 89 | + |
| 90 | + return array.count(Field.Visited) |
| 91 | + } |
| 92 | + |
| 93 | + fun part2(input: List<String>): Int { |
| 94 | + return TODO() |
| 95 | + } |
| 96 | + |
| 97 | + val testInput = readInput("Day06_test") |
| 98 | + val input = readInput("Day06") |
| 99 | + |
| 100 | + test(41) { part1(testInput) } |
| 101 | + exec { part1(input) } |
| 102 | + test(TODO()) { part2(testInput) } |
| 103 | + exec { part2(input) } |
| 104 | +} |
0 commit comments