Skip to content

Commit

Permalink
2024 - Day 24 - part 1 - hm: not caching result of buildNode
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 24, 2024
1 parent 3ed574a commit 98b36d6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
35 changes: 31 additions & 4 deletions src/main/kotlin/no/rodland/advent_2024/Day24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ import no.rodland.advent.Day
// template generated: 24/12/2024
// Fredrik Rødland 2024

class Day24(val input: List<String>) : Day<Long, Long, Pair<List<Day24.Wire>, Map<String, String>>> {
class Day24(val input: List<String>) : Day<Long, Long, Pair<Map<String, Day24.Wire>, Map<String, String>>> {

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<String, Wire>, input: Map<String, String>, 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 {
Expand Down Expand Up @@ -50,9 +77,9 @@ class Day24(val input: List<String>) : Day<Long, Long, Pair<List<Day24.Wire>, Ma
//x00 AND y00 -> z00
//x01 XOR y01 -> z01
//x02 OR y02 -> z02
override fun List<String>.parse(): Pair<List<Wire>, Map<String, String>> {
override fun List<String>.parse(): Pair<Map<String, Wire>, Map<String, String>> {
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
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/no/rodland/advent_2024/Day24Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -29,6 +29,7 @@ internal class Day24Test {
resultTwo,
{ Day24(data24) },
{ Day24(test24) },
numTestPart1 = 1
)

@Nested
Expand Down
49 changes: 43 additions & 6 deletions src/test/resources/2024/input_24_test.txt
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit 98b36d6

Please sign in to comment.