From 50a8574a101d2ec5d689e2659ca6aa85421f4931 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Fri, 22 Dec 2023 07:36:39 +0100 Subject: [PATCH] Part 1 at least. --- src/aoc2023/Day22.kt | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/aoc2023/Day22.kt b/src/aoc2023/Day22.kt index 679d6aa..f3f4aa6 100644 --- a/src/aoc2023/Day22.kt +++ b/src/aoc2023/Day22.kt @@ -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, width: Int, height: Int): Int { + fun parts(input: List, width: Int, height: Int): Pair { 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() - 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): Int { - return 0 + val fallingSet = HashSet() + //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() }