diff --git a/src/aoc2023/Day13.kt b/src/aoc2023/Day13.kt index 028ba46..8a34f2a 100644 --- a/src/aoc2023/Day13.kt +++ b/src/aoc2023/Day13.kt @@ -3,6 +3,7 @@ package aoc2023 import CharGrid import println import readInput +import kotlin.math.min /* --- Day 13: Point of Incidence --- @@ -85,31 +86,31 @@ fun main() { tmpList.add(i) } else { val grid = CharGrid(tmpList) - val ha = IntArray(grid.width) - val haRev = IntArray(grid.width) - val va = IntArray(grid.height) - val vaRev = IntArray(grid.height) + val vertSA = IntArray(grid.width) + val vertSARev = IntArray(grid.width) + val horzSA = IntArray(grid.height) + val horzSARev = IntArray(grid.height) for (c in 0 until grid.width) { for (r in 0 until grid.height) { val revR = grid.height - 1 - r - ha[c] = ha[c] * 2 + (if (grid[c, r] == '#') 1 else 0) - haRev[c] = haRev[c] * 2 + if (grid[c, revR] == '#') 1 else 0 + vertSA[c] = vertSA[c] * 2 + (if (grid[c, r] == '#') 1 else 0) + vertSARev[c] = vertSARev[c] * 2 + if (grid[c, revR] == '#') 1 else 0 } } for (r in 0 until grid.height) { for (c in 0 until grid.width) { val revC = grid.width - 1 - c - va[r] = va[r] * 2 + if (grid[c, r] == '#') 1 else 0 - vaRev[r] = vaRev[r] * 2 + if (grid[revC, r] == '#') 1 else 0 + horzSA[r] = horzSA[r] * 2 + if (grid[c, r] == '#') 1 else 0 + horzSARev[r] = horzSARev[r] * 2 + if (grid[revC, r] == '#') 1 else 0 } } var href = 0 - for (sh in grid.height - 2 downTo 1) { + for (sh in 1 until grid.height-1) { val mask = ((1 shl grid.height) - 1) shr sh var found = false for (c in 0 until grid.width) { - if ((ha[c] shr sh) and mask != haRev[c] and mask) { + if ((vertSA[c] shr sh) and mask != vertSARev[c] and mask) { found = false break } @@ -120,11 +121,12 @@ fun main() { } var vref = 0 - for (sh in grid.width - 2 downTo 1) { - val mask = ((1 shl grid.width) - 1) shr sh + for (sh in 1 until grid.width-1) { + val width = min(sh, grid.width-sh) + val mask = (1 shl width) - 1 var found = false for (r in 0 until grid.height) { - if ((va[r] shr sh) and mask != vaRev[r] and mask) { + if (((horzSA[r] shr ((sh-width).coerceAtLeast(0))) and mask) != ((horzSARev[r] shr (grid.width-width).coerceAtMost(0)) and mask)) { found = false break } @@ -133,6 +135,10 @@ fun main() { vref = sh } } + for(r in 0 until grid.height) + { + println(horzSA[r].toString(2).padStart(grid.width, '0') + " " + horzSARev[r].toString(2).padStart(grid.width, '0')) + } tmpList.clear() }