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 {
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 (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())
@ -175,30 +179,27 @@ fun main() {
brickTopZ[y * width + x] = flatTopZ + b.h()
}
}
brickTopZ.asSequence().chunked(width).joinToString("\n") { it.joinToString(" ") { String.format("%3d", it) } }.println()
println()
//brickTopZ.asSequence().chunked(width).joinToString("\n") { it.joinToString(" ") { String.format("%3d", it) } }.println()
//println()
}
val supportSuperSet = 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) })
return bricks.count { it.uponBricks.size != 1 }
}
fun part2(input: List<String>): Int {
return 0
val fallingSet = HashSet<Brick>()
//println(bricks.map { "%c: %d".format(('A' + it.id), it.onTopBricks.size) })
val disBricks = bricks.filterNot { it.onTopBricks.all { 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()
return safeBricks to fallingBricks
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2023/Day22_test")
val testInputPart1Result = part1(testInput, 3, 3)
val (testInputPart1Result, testInputPart2Result) = parts(testInput, 3, 3)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 5)
check(testInputPart2Result == 0)
check(testInputPart2Result == 7)
val input = readInput("aoc2023/Day22")
part1(input, 10, 10).println()
part2(input).println()
parts(input, 10, 10).println()
}