Part 1 at least.

This commit is contained in:
Chris Hodges 2023-12-22 07:36:39 +01:00
parent 8f1e9e238e
commit 50a8574a10

View File

@ -144,9 +144,13 @@ fun main() {
override fun hashCode(): Int { override fun hashCode(): Int {
return id return id
} }
override fun toString(): String {
return "Brick(id=$id, uponBricks=$uponBricks)"
}
} }
fun part1(input: List<String>, width: Int, height: Int): Int { fun parts(input: List<String>, width: Int, height: Int): Pair<Int, Int> {
val bricks = input.mapIndexed { id, it -> val bricks = input.mapIndexed { id, it ->
val (x1, y1, z1, x2, y2, z2) = "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)".toRegex().matchEntire(it)?.destructured!! val (x1, y1, z1, x2, y2, z2) = "(\\d+),(\\d+),(\\d+)~(\\d+),(\\d+),(\\d+)".toRegex().matchEntire(it)?.destructured!!
Brick(id, x1.toInt(), y1.toInt(), z1.toInt(), x2.toInt(), y2.toInt(), z2.toInt()) Brick(id, x1.toInt(), y1.toInt(), z1.toInt(), x2.toInt(), y2.toInt(), z2.toInt())
@ -175,30 +179,27 @@ fun main() {
brickTopZ[y * width + x] = flatTopZ + b.h() brickTopZ[y * width + x] = flatTopZ + b.h()
} }
} }
brickTopZ.asSequence().chunked(width).joinToString("\n") { it.joinToString(" ") { String.format("%3d", it) } }.println() //brickTopZ.asSequence().chunked(width).joinToString("\n") { it.joinToString(" ") { String.format("%3d", it) } }.println()
println() //println()
} }
val supportSuperSet = HashSet<Brick>() val fallingSet = HashSet<Brick>()
bricks.forEach { if (it.onTopBricks.size != 1) supportSuperSet.addAll(it.onTopBricks) } //println(bricks.map { "%c: %d".format(('A' + it.id), it.onTopBricks.size) })
println(bricks.map { "%c: %d".format(('A' + it.id), it.onTopBricks.size) }) val disBricks = bricks.filterNot { it.onTopBricks.all { it.uponBricks.size != 1 } }
return bricks.count { it.uponBricks.size != 1 } val safeBricks = bricks.size - disBricks.size
} println(disBricks)
val fallingBricks = disBricks.map { it.onTopBricks.filter { it.uponBricks.size < 2 }.size }.sum()
fun part2(input: List<String>): Int { return safeBricks to fallingBricks
return 0
} }
// test if implementation meets criteria from the description, like: // test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines() val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2023/Day22_test") //val testInput = readInput("aoc2023/Day22_test")
val testInputPart1Result = part1(testInput, 3, 3) val (testInputPart1Result, testInputPart2Result) = parts(testInput, 3, 3)
println("Part 1 Test: $testInputPart1Result") println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result") println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 5) check(testInputPart1Result == 5)
check(testInputPart2Result == 0) check(testInputPart2Result == 7)
val input = readInput("aoc2023/Day22") val input = readInput("aoc2023/Day22")
part1(input, 10, 10).println() parts(input, 10, 10).println()
part2(input).println()
} }