Change-Id: I50b8abdf506c7899dea213d0cf8ec79500ce795b
This commit is contained in:
Chris Hodges 2023-12-14 17:51:00 +01:00
parent 226e61a203
commit b3f648a01e

View File

@ -3,6 +3,7 @@ package aoc2023
import CharGrid import CharGrid
import println import println
import readInput import readInput
import kotlin.math.min
/* /*
--- Day 13: Point of Incidence --- --- Day 13: Point of Incidence ---
@ -85,31 +86,31 @@ fun main() {
tmpList.add(i) tmpList.add(i)
} else { } else {
val grid = CharGrid(tmpList) val grid = CharGrid(tmpList)
val ha = IntArray(grid.width) val vertSA = IntArray(grid.width)
val haRev = IntArray(grid.width) val vertSARev = IntArray(grid.width)
val va = IntArray(grid.height) val horzSA = IntArray(grid.height)
val vaRev = IntArray(grid.height) val horzSARev = IntArray(grid.height)
for (c in 0 until grid.width) { for (c in 0 until grid.width) {
for (r in 0 until grid.height) { for (r in 0 until grid.height) {
val revR = grid.height - 1 - r val revR = grid.height - 1 - r
ha[c] = ha[c] * 2 + (if (grid[c, r] == '#') 1 else 0) vertSA[c] = vertSA[c] * 2 + (if (grid[c, r] == '#') 1 else 0)
haRev[c] = haRev[c] * 2 + if (grid[c, revR] == '#') 1 else 0 vertSARev[c] = vertSARev[c] * 2 + if (grid[c, revR] == '#') 1 else 0
} }
} }
for (r in 0 until grid.height) { for (r in 0 until grid.height) {
for (c in 0 until grid.width) { for (c in 0 until grid.width) {
val revC = grid.width - 1 - c val revC = grid.width - 1 - c
va[r] = va[r] * 2 + if (grid[c, r] == '#') 1 else 0 horzSA[r] = horzSA[r] * 2 + if (grid[c, r] == '#') 1 else 0
vaRev[r] = vaRev[r] * 2 + if (grid[revC, r] == '#') 1 else 0 horzSARev[r] = horzSARev[r] * 2 + if (grid[revC, r] == '#') 1 else 0
} }
} }
var href = 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 val mask = ((1 shl grid.height) - 1) shr sh
var found = false var found = false
for (c in 0 until grid.width) { 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 found = false
break break
} }
@ -120,11 +121,12 @@ fun main() {
} }
var vref = 0 var vref = 0
for (sh in grid.width - 2 downTo 1) { for (sh in 1 until grid.width-1) {
val mask = ((1 shl grid.width) - 1) shr sh val width = min(sh, grid.width-sh)
val mask = (1 shl width) - 1
var found = false var found = false
for (r in 0 until grid.height) { 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 found = false
break break
} }
@ -133,6 +135,10 @@ fun main() {
vref = sh 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() tmpList.clear()
} }