Skip to content

Commit

Permalink
2024 - Day 24 - started parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fmmr committed Dec 24, 2024
1 parent ce0e9ba commit 3ed574a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 48 deletions.
48 changes: 43 additions & 5 deletions src/main/kotlin/no/rodland/advent_2024/Day24.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import no.rodland.advent.Day
// template generated: 24/12/2024
// Fredrik Rødland 2024

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

private val parsed = input.parse()

Expand All @@ -17,11 +17,49 @@ class Day24(val input: List<String>) : Day<Long, Long, List<String>> {
return 2
}

override fun List<String>.parse(): List<String> {
return map { line ->
line
}
sealed interface Op {
val value: Boolean
}

data class VALUE(override var value: Boolean) : Op

data class AND(var a: Wire, var b: Wire) : Op {
override val value: Boolean
get() = a.input.value && b.input.value
}

data class OR(var a: Wire, var b: Wire) : Op {
override val value: Boolean
get() = a.input.value || b.input.value
}

data class XOR(var a: Wire, var b: Wire) : Op {
override val value: Boolean
get() = a.input.value xor b.input.value
}

data class Wire(val name: String, var input: Op) {}

//x00: 1
//x01: 1
//x02: 1
//y00: 0
//y01: 1
//y02: 0
//
//x00 AND y00 -> z00
//x01 XOR y01 -> z01
//x02 OR y02 -> z02
override fun List<String>.parse(): Pair<List<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 logic = second.lines().associate { line -> line.split(" -> ").let { it[1] to it[0] } }

return values to logic
}

private fun String.toB() = this == "1"

override val day = "24".toInt()
}

49 changes: 6 additions & 43 deletions src/test/resources/2024/input_24_test.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,10 @@
x00: 1
x01: 0
x01: 1
x02: 1
x03: 1
x04: 0
y00: 1
y00: 0
y01: 1
y02: 1
y03: 1
y04: 1
y02: 0

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
x00 AND y00 -> z00
x01 XOR y01 -> z01
x02 OR y02 -> z02

0 comments on commit 3ed574a

Please sign in to comment.