From 98b36d6bd02e90bcef138378d91e6dcc74771d1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Tue, 24 Dec 2024 12:29:23 +0100 Subject: [PATCH] 2024 - Day 24 - part 1 - hm: not caching result of buildNode --- .../kotlin/no/rodland/advent_2024/Day24.kt | 35 +++++++++++-- .../no/rodland/advent_2024/Day24Test.kt | 5 +- src/test/resources/2024/input_24_test.txt | 49 ++++++++++++++++--- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/no/rodland/advent_2024/Day24.kt b/src/main/kotlin/no/rodland/advent_2024/Day24.kt index 6e4d8da0..21ff6f7d 100644 --- a/src/main/kotlin/no/rodland/advent_2024/Day24.kt +++ b/src/main/kotlin/no/rodland/advent_2024/Day24.kt @@ -5,12 +5,39 @@ import no.rodland.advent.Day // template generated: 24/12/2024 // Fredrik Rødland 2024 -class Day24(val input: List) : Day, Map>> { +class Day24(val input: List) : Day, Map>> { private val parsed = input.parse() override fun partOne(): Long { - return 2 + val (values, logic) = parsed + val system = values.toMutableMap() + + val map = logic + .map { (k, v) -> buildNode(system, logic, k, v) } + println("hm - ran through buildNode: $i") + return map + .filter { it.name.startsWith("z") } + .sortedByDescending { it.name } + .map { it.input.value } + .joinToString("") { if (it) "1" else "0" } + .toLong(2) + } + + var i = 0 + private fun buildNode(system: MutableMap, input: Map, k: String, v: String): Wire { + system[k]?.let { wire -> return wire } + i++ + val (aKey, op, bKey) = v.split(" ") + val a = system[aKey] ?: buildNode(system, input, aKey, input[aKey]!!) + val b = system[bKey] ?: buildNode(system, input, aKey, input[bKey]!!) + val wire = when (op) { + "AND" -> Wire(k, AND(a, b)) + "OR" -> Wire(k, OR(a, b)) + "XOR" -> Wire(k, XOR(a, b)) + else -> error("Unknown op $op") + } + return wire } override fun partTwo(): Long { @@ -50,9 +77,9 @@ class Day24(val input: List) : Day, Ma //x00 AND y00 -> z00 //x01 XOR y01 -> z01 //x02 OR y02 -> z02 - override fun List.parse(): Pair, Map> { + override fun List.parse(): Pair, Map> { val (first, second) = joinToString("\n").split("\n\n") - val values = first.lines().map { it.split(": ").let { it[0] to it[1].toB() } }.map { (name, value) -> Wire(name, VALUE(value)) } + val values = first.lines().map { it.split(": ").let { it[0] to it[1].toB() } }.map { (name, value) -> name to Wire(name, VALUE(value)) }.toMap() val logic = second.lines().associate { line -> line.split(" -> ").let { it[1] to it[0] } } return values to logic diff --git a/src/test/kotlin/no/rodland/advent_2024/Day24Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day24Test.kt index 9ce27ec0..adb04d4c 100644 --- a/src/test/kotlin/no/rodland/advent_2024/Day24Test.kt +++ b/src/test/kotlin/no/rodland/advent_2024/Day24Test.kt @@ -15,9 +15,9 @@ internal class Day24Test { private val data24 = "2024/input_24.txt".readFile() private val test24 = "2024/input_24_test.txt".readFile() - private val resultTestOne = 2L + private val resultTestOne = 2024L private val resultTestTwo = 2L - private val resultOne = 2L + private val resultOne = 57344080719736L // 8510417647883 too low private val resultTwo = 2L val test = defaultTestSuiteParseOnInit( @@ -29,6 +29,7 @@ internal class Day24Test { resultTwo, { Day24(data24) }, { Day24(test24) }, + numTestPart1 = 1 ) @Nested diff --git a/src/test/resources/2024/input_24_test.txt b/src/test/resources/2024/input_24_test.txt index de04a48f..09fb2305 100644 --- a/src/test/resources/2024/input_24_test.txt +++ b/src/test/resources/2024/input_24_test.txt @@ -1,10 +1,47 @@ x00: 1 -x01: 1 +x01: 0 x02: 1 -y00: 0 +x03: 1 +x04: 0 +y00: 1 y01: 1 -y02: 0 +y02: 1 +y03: 1 +y04: 1 -x00 AND y00 -> z00 -x01 XOR y01 -> z01 -x02 OR y02 -> z02 \ No newline at end of file +ntg XOR fgs -> mjb +y02 OR x01 -> tnw +kwq OR kpj -> z05 +x00 OR x03 -> fst +tgd XOR rvg -> z01 +vdt OR tnw -> bfw +bfw AND frj -> z10 +ffh OR nrd -> bqk +y00 AND y03 -> djm +y03 OR y00 -> psh +bqk OR frj -> z08 +tnw OR fst -> frj +gnj AND tgd -> z11 +bfw XOR mjb -> z00 +x03 OR x00 -> vdt +gnj AND wpb -> z02 +x04 AND y00 -> kjc +djm OR pbm -> qhw +nrd AND vdt -> hwm +kjc AND fst -> rvg +y04 OR y02 -> fgs +y01 AND x02 -> pbm +ntg OR kjc -> kwq +psh XOR fgs -> tgd +qhw XOR tgd -> z09 +pbm OR djm -> kpj +x03 XOR y03 -> ffh +x00 XOR y04 -> ntg +bfw OR bqk -> z06 +nrd XOR fgs -> wpb +frj XOR qhw -> z04 +bqk OR frj -> z07 +y03 OR x01 -> nrd +hwm AND bqk -> z03 +tgd XOR rvg -> z12 +tnw OR pbm -> gnj \ No newline at end of file