Reworked day 3, adding a generic class for 2D grids with some special functions for day 3. Modified template to avoid double evaluation.

This commit is contained in:
Chris Hodges 2023-12-03 15:21:59 +01:00
parent 077fbe6e21
commit afcfed70d7
3 changed files with 174 additions and 127 deletions

View File

@ -23,10 +23,12 @@ ${testinput}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("${package}/Day${day}_test")
println("Part 1 Test: " + part1(testInput))
println("Part 2 Test: " + part2(testInput))
check(part1(testInput) == 0)
check(part2(testInput) == 0)
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 0)
check(testInputPart2Result == 0)
val input = readInput("${package}/Day${day}")
part1(input).println()

View File

@ -25,24 +25,128 @@ fun listOfIntegerLists(input: List<String>): ArrayList<List<Int>> {
return output
}
fun squareLinearizedOneDigitArray(input: List<String>): IntArray {
val dim = input.first().length
val out = IntArray(dim * dim)
for ((rnum, r) in input.withIndex()) {
r.forEachIndexed { index, c -> out[rnum * dim + index] = c - '0' }
}
return out
data class RelPos(val dc: Int, val dr: Int) {
fun translate(c: Int, r: Int) = RelPos(c + dc, r + dr)
}
fun twoDOneDigitArray(input: List<String>): Array<IntArray> {
return Array(input.size) { i ->
with(input[i]) {
val a = IntArray(length)
for (c in indices) {
a[c] = get(c).digitToInt()
class CharGrid {
companion object {
val PLUS_POS = listOf(RelPos(0, -1), RelPos(-1, 0), RelPos(1, 0), RelPos(0, 1))
val PLUS_CENTER_POS = listOf(RelPos(0, -1), RelPos(-1, 0), RelPos(0, 0), RelPos(1, 0), RelPos(0, 1))
val CROSS_POS = listOf(RelPos(-1, -1), RelPos(1, -1), RelPos(-1, 1), RelPos(1, 1))
val CROSS_CENTER_POS = listOf(RelPos(-1, -1), RelPos(1, -1), RelPos(0, 0), RelPos(-1, 1), RelPos(1, 1))
val BOX_POS = listOf(
RelPos(-1, -1), RelPos(0, -1), RelPos(1, -1),
RelPos(-1, 0), RelPos(1, 0),
RelPos(-1, 1), RelPos(0, 1), RelPos(1, 1)
)
val NINE_POS = listOf(
RelPos(-1, -1), RelPos(0, -1), RelPos(1, -1),
RelPos(-1, 0), RelPos(0, 0), RelPos(1, 0),
RelPos(-1, 1), RelPos(0, 1), RelPos(1, 1)
)
}
a
val width: Int
val height: Int
val data: Array<CharArray>
val bChar: Char
constructor(input: List<String>, borderChar: Char = ' ') {
bChar = borderChar
width = input[0].length
height = input.size
data = Array(height) { input[it].toCharArray() }
}
constructor(inputGrid: Array<CharArray>, borderChar: Char = ' ') {
bChar = borderChar
this.data = inputGrid
width = inputGrid[0].size
height = inputGrid.size
}
operator fun get(col: Int, row: Int) = getOrNull(col, row) ?: bChar
operator fun set(col: Int, row: Int, newChar: Char?) {
if (newChar != null && isInside(col, row)) data[row][col] = newChar
}
fun setOrThrow(col: Int, row: Int, newChar: Char) {
if (!isInside(col, row)) throw IndexOutOfBoundsException("$col, $row out of bounds")
set(col, row, newChar)
}
fun getOrNull(col: Int, row: Int) = if (isInside(col, row)) data[row][col] else null
fun isInside(col: Int, row: Int) = (col in 0 until width) && (row in 0 until height)
fun getHorizNumberValueAt(rp: RelPos) = getHorizNumberValueAt(rp.dc, rp.dr)
fun getHorizNumberValueAt(col: Int, row: Int): Pair<Int, List<RelPos>> {
val relPos = getHorizNumberRelPosAt(col, row)
return relPos.map { this[col + it.dc, row + it.dr].digitToInt() }.reduce { acc, digit -> acc * 10 + digit } to relPos
}
fun getHorizNumberRelPosAt(col: Int, row: Int): List<RelPos> {
val relPos = ArrayList<RelPos>(5)
var c = 0
while (col + c > 0 && get(col + c - 1, row).isDigit()) c--
while (col + c < width && get(col + c, row).isDigit()) {
relPos.add(RelPos(c, 0))
c++
}
return relPos
}
fun copyOf() = CharGrid(Array(height) { data[it].copyOf() }, borderChar = bChar)
fun applyWithPos(op: (grid: CharGrid, col: Int, row: Int) -> Char?) {
for (r in 0 until height) {
for (c in 0 until width) {
this[c, r] = op(this, c, r)
}
}
}
fun apply(op: (content: Char) -> Char?) {
applyWithPos { grid: CharGrid, col, row -> op(grid[col, row]) }
}
fun grow(
relposes: Iterable<RelPos>, predicate: (content: Char) -> Boolean,
op: (orgContent: Char, oldContent: Char, col: Int, row: Int) -> Char? = { orgContent: Char, _: Char, _: Int, _: Int -> orgContent }
): CharGrid {
val newGrid = copyOf()
for (r in 0 until height) {
for (c in 0 until width) {
if (predicate(get(c, r))) {
relposes
.map { it.translate(c, r) }
.filter { isInside(it.dc, it.dr) }
.forEach { newGrid[it.dc, it.dr] = op(this[c, r], newGrid[it.dc, it.dr], it.dc, it.dr) }
}
}
}
return newGrid
}
fun collect(c: Int, r: Int, relposes: Iterable<RelPos>, skipBorder: Boolean = true): List<Char> =
if (skipBorder) {
relposes.mapNotNull { getOrNull(c + it.dc, r + it.dr) }
} else {
relposes.map { get(c + it.dc, r + it.dr) }
}
fun addBorder(borderChar: Char = bChar): CharGrid {
val topBottom = CharArray(width + 2) { borderChar }
return CharGrid(Array(height + 2) {
if (it == 0 || it == height + 1) topBottom else
CharArray(width + 2) { r -> if (r == 0 || r == width + 1) borderChar else get(it - 1, r - 1) }
}, borderChar)
}
fun debug() {
data.joinToString("\n") { it.concatToString() }.println()
}
}

View File

@ -1,5 +1,6 @@
package aoc2023
import CharGrid
import println
import readInput
@ -25,6 +26,26 @@ Here is an example engine schematic:
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
--- Part Two ---
The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source.
You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers.
Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola.
The missing part wasn't the only issue - one of the gears in the engine is wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its gear ratio is the result of multiplying those two numbers together.
This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced.
Consider the same engine schematic again:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, there are two gears. The first is in the top left; it has part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces 467835.
What is the sum of all of the gear ratios in your engine schematic?
*/
fun main() {
@ -42,121 +63,39 @@ fun main() {
.664.598..
"""
fun squareLinearizedArray(input: List<String>): Pair<IntArray, CharArray> {
val dim = input.first().length
val out = IntArray(dim * dim)
val symbols = CharArray(dim * dim)
for ((rnum, r) in input.withIndex()) {
r.forEachIndexed { index, c -> out[rnum * dim + index] = if (c.isDigit()) c - '0' else -1 }
r.forEachIndexed { index, c -> symbols[rnum * dim + index] = if (!c.isDigit()) c else '.' }
}
return out to symbols
}
fun part1(input: List<String>): Int {
val dim = input.first().length
val (out, symbols) = squareLinearizedArray(input)
val wholeGrid = CharGrid(input)
val symbolGrid = wholeGrid.copyOf()
symbolGrid.apply { c -> if (!c.isDigit()) c else '.' }
val grownGrid = symbolGrid.grow(CharGrid.BOX_POS, { c -> c != '.' })
var sum = 0
val adjMap = BooleanArray(dim * dim)
for (i in out.indices) {
val x = i % dim
val y = i / dim
adjMap[i] = (x > 0 && (symbols[i - 1] != '.')) ||
(x < dim - 1 && (symbols[i + 1] != '.')) ||
(y > 0 && x > 0 && (symbols[i - dim - 1] != '.')) ||
(y > 0 && (symbols[i - dim] != '.')) ||
(y > 0 && x < dim - 1 && (symbols[i - dim + 1] != '.')) ||
(y < dim - 1 && x > 0 && (symbols[i + dim - 1] != '.')) ||
(y < dim - 1 && (symbols[i + dim] != '.')) ||
(y < dim - 1 && x < dim - 1 && (symbols[i + dim + 1] != '.'))
wholeGrid.applyWithPos { grid, col, row ->
if (grid[col, row].isDigit() && grownGrid[col, row] != '.') {
val (num, relpos) = grid.getHorizNumberValueAt(col, row)
relpos.forEach { grid[col + it.dc, row + it.dr] = '.' }
sum += num
}
// adjMap.asSequence().chunked(dim).joinToString("\n") { it.joinToString("") { if (it) "*" else " " } }.println()
var num = 0
var pickMe = false
for (i in out.indices) {
if (out[i] < 0 || i % dim == 0) {
if (pickMe) sum += num
pickMe = false
num = 0
null
}
if (out[i] >= 0) {
pickMe = pickMe or adjMap[i]
num *= 10
num += out[i]
}
}
if (pickMe) sum += num
return sum
}
fun part2(input: List<String>): Int {
val dim = input.first().length
val (out, symbols) = squareLinearizedArray(input)
val wholeGrid = CharGrid(input)
var sum = 0
val adjMap = BooleanArray(dim * dim)
for (i in out.indices) {
val x = i % dim
val y = i / dim
if (symbols[i] == '*') {
var digaround = 0
val posMaps = ArrayList<Pair<Int, Int>>()
if (x > 0 && (out[i - 1] >= 0)) {
digaround += 1
posMaps.add(-1 to 0)
}
if (x < dim - 1 && (out[i + 1] >= 0)) {
digaround += 1
posMaps.add(+1 to 0)
}
var digtop = 0
if (y > 0 && x > 0 && (out[i - dim - 1] >= 0)) digtop += 1
if (y > 0 && (out[i - dim] >= 0)) digtop += 2
if (y > 0 && x < dim - 1 && (out[i - dim + 1] >= 0)) digtop += 4
if (listOf(1, 2, 3, 4, 6, 7).contains(digtop)) {
digaround += 1
if (digtop and 1 != 0) posMaps.add(-1 to -1)
else if (digtop and 2 != 0) posMaps.add(0 to -1)
else if (digtop and 4 != 0) posMaps.add(1 to -1)
}
if (listOf(5).contains(digtop)) {
digaround += 2
posMaps.add(-1 to -1)
posMaps.add(1 to -1)
}
var digbottom = 0
if (y < dim - 1 && x > 0 && (out[i + dim - 1] >= 0)) digbottom += 1
if (y < dim - 1 && (out[i + dim] >= 0)) digbottom += 2
if (y < dim - 1 && x < dim - 1 && (out[i + dim + 1] >= 0)) digbottom += 4
if (listOf(1, 2, 3, 4, 6, 7).contains(digbottom)) {
digaround += 1
if (digbottom and 1 != 0) posMaps.add(-1 to 1)
else if (digbottom and 2 != 0) posMaps.add(0 to 1)
else if (digbottom and 4 != 0) posMaps.add(1 to 1)
}
if (listOf(5).contains(digbottom)) {
digaround += 2
posMaps.add(-1 to 1)
posMaps.add(1 to 1)
}
if (digaround == 2) {
val numbers = posMaps.map {
var xx = x + it.first
var yy = y + it.second
while (xx > 0 && out[xx - 1 + yy * dim] >= 0) xx--
var num = 0
while (out[xx + yy * dim] >= 0 && xx < dim) {
num *= 10
num += out[xx + yy * dim]
xx++
}
num
}
sum += numbers[0] * numbers[1]
wholeGrid.applyWithPos { grid, col, row ->
if (grid[col, row] == '*') {
val numbers = CharGrid.BOX_POS.asSequence()
.map { it.translate(col, row) }
.filter { grid[it.dc, it.dr].isDigit() }
.map { grid.getHorizNumberRelPosAt(it.dc, it.dr)[0].translate(it.dc, it.dr) }
.distinct()
.toList()
if (numbers.size == 2) {
sum += grid.getHorizNumberValueAt(numbers[0]).first * grid.getHorizNumberValueAt(numbers[1]).first
}
}
null
}
return sum
}
@ -164,10 +103,12 @@ fun main() {
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2023/Day03_test")
println("Part 1 Test: " + part1(testInput))
println("Part 2 Test: " + part2(testInput))
check(part1(testInput) == 4361)
check(part2(testInput) == 467835)
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 4361)
check(testInputPart2Result == 467835)
val input = readInput("aoc2023/Day03")
part1(input).println()