|
1 | 1 | package tr.emreone.adventofcode.days |
2 | 2 |
|
3 | 3 | import tr.emreone.kotlin_utils.automation.Day |
| 4 | +import java.lang.IllegalStateException |
4 | 5 |
|
5 | 6 | class Day13 : Day(13, 2023, "Point of Incidence") { |
6 | 7 |
|
| 8 | + class Grid(input: String) { |
| 9 | + val area: List<List<Char>> |
| 10 | + var horizontalLineIndex: Int? = null |
| 11 | + var verticalLineIndex: Int? = null |
| 12 | + |
| 13 | + init { |
| 14 | + this.area = input.split("\\r?\\n".toRegex()) |
| 15 | + .map { |
| 16 | + it.toCharArray().map { it } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + private fun getColumn(x: Int): List<Char> { |
| 21 | + if (x !in this.area.get(0).indices) { |
| 22 | + throw IndexOutOfBoundsException() |
| 23 | + } |
| 24 | + return (this.area).map { |
| 25 | + it[x] |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + private fun getRow(y: Int): List<Char> { |
| 30 | + return this.area.get(y) |
| 31 | + } |
| 32 | + |
| 33 | + private fun isVerticalReflectedAt(yi: Int): Boolean { |
| 34 | + if (yi < 0 || yi == this.area.lastIndex) return false |
| 35 | + |
| 36 | + val maxHeight = minOf(yi, this.area.lastIndex - 1 - yi) |
| 37 | + return (0..maxHeight).all { i -> |
| 38 | + val row1 = getRow(yi - i) |
| 39 | + val row2 = getRow(yi + 1 + i) |
| 40 | + // println("Comparing y=${yi - i} ($row1) with y=${yi + 1 + i} ($row2)") |
| 41 | + row1.equals(row2) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private fun isHorizontalReflectedAt(xi: Int): Boolean { |
| 46 | + if (xi < 0 || xi == this.area.get(0).lastIndex) return false |
| 47 | + |
| 48 | + val maxWidth = minOf(xi, this.area.get(0).lastIndex - 1 - xi) |
| 49 | + return (0..maxWidth).all { i -> |
| 50 | + val col1 = getColumn(xi - i) |
| 51 | + val col2 = getColumn(xi + 1 + i) |
| 52 | + // println("Comparing x=${xi - i} ($col1) with x=${xi + 1 + i} ($col2)") |
| 53 | + col1.equals(col2) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fun getScore(originalGrid: Grid? = null): Int { |
| 58 | + val possibleVerticalLines = (0 until this.area.lastIndex).filter { |
| 59 | + isVerticalReflectedAt(it) |
| 60 | + } |
| 61 | + if (possibleVerticalLines.isNotEmpty()) { |
| 62 | + this.verticalLineIndex = possibleVerticalLines.firstOrNull { |
| 63 | + it != originalGrid?.verticalLineIndex |
| 64 | + } |
| 65 | + if (this.verticalLineIndex != null) { |
| 66 | + return (this.verticalLineIndex!! + 1) * 100 |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + val possibleHorizontalLines = (0 until this.area.get(0).lastIndex).filter { |
| 71 | + isHorizontalReflectedAt(it) |
| 72 | + } |
| 73 | + if (possibleHorizontalLines.isNotEmpty()) { |
| 74 | + this.horizontalLineIndex = possibleHorizontalLines.firstOrNull { |
| 75 | + it != originalGrid?.horizontalLineIndex |
| 76 | + } |
| 77 | + if (this.horizontalLineIndex != null) { |
| 78 | + return this.horizontalLineIndex!! + 1 |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return 0 |
| 83 | + } |
| 84 | + |
| 85 | + fun getCopyWithToggledCell(xi: Int, yi: Int): Grid { |
| 86 | + val aGrid = this.area.map { it.toMutableList() } |
| 87 | + aGrid[yi][xi] = if (aGrid[yi][xi] == '.') '#' else '.' |
| 88 | + |
| 89 | + return Grid(aGrid.joinToString("\n") { it.joinToString("") }) |
| 90 | + } |
| 91 | + |
| 92 | + fun getAlternativeScore(): Int { |
| 93 | + for(yi in this.area.indices) { |
| 94 | + for(xi in this.area.get(yi).indices) { |
| 95 | + val alternativeGrid = getCopyWithToggledCell(xi, yi) |
| 96 | + val aScore = alternativeGrid.getScore(this) |
| 97 | + |
| 98 | + if (aScore > 0) { |
| 99 | + return aScore |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + throw IllegalStateException("No alternative score found.") |
| 105 | + } |
| 106 | + |
| 107 | + fun print() { |
| 108 | + for(y in this.area.indices) { |
| 109 | + println(this.area[y].joinToString("")) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + override fun part1(): Int { |
| 115 | + return inputAsString.split("(\\r?\\n){2}".toRegex()) |
| 116 | + .sumOf { |
| 117 | + Grid(it).getScore() |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + override fun part2(): Int { |
| 122 | + return inputAsString.split("(\\r?\\n){2}".toRegex()) |
| 123 | + .sumOf { |
| 124 | + val grid = Grid(it) |
| 125 | + val score = grid.getScore() |
| 126 | + grid.getAlternativeScore() |
| 127 | + } |
| 128 | + } |
| 129 | + |
7 | 130 | } |
0 commit comments