Skip to content

Commit 62912a6

Browse files
committed
Solve 2024 day 24 part 2 manually
1 parent 2392b2d commit 62912a6

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day24.scala

+42-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ object Day24 {
4545
})
4646
}
4747

48+
def swap(circuit: Circuit, name1: String, name2: String): Circuit =
49+
circuit + (name1 -> circuit(name2)) + (name2 -> circuit(name1))
50+
51+
def changeInput(circuit: Circuit, prefix: String, value: Long): Circuit = {
52+
val (a, b) = circuit.keys
53+
.filter(_.startsWith(prefix))
54+
.toSeq
55+
.sorted
56+
.foldLeft((circuit, value))({ case ((circuit, value), prefixName) =>
57+
(circuit + (prefixName -> Wire.Input((value & 1) == 1L)), value >> 1)
58+
})
59+
assert(b == 0)
60+
a
61+
}
62+
4863
def parseInput(s: String): (String, Wire.Input) = s match {
4964
case s"$name: 0" => name -> Wire.Input(false)
5065
case s"$name: 1" => name -> Wire.Input(true)
@@ -63,9 +78,35 @@ object Day24 {
6378
inputMap ++ gateMap
6479
}
6580

81+
def printCircuitDot(circuit: Circuit): Unit = {
82+
println("digraph circuit {")
83+
for ((name, wire) <- circuit) {
84+
wire match {
85+
case Wire.Input(value) =>
86+
println(s" $name;")
87+
case Wire.Gate(lhs, op, rhs) =>
88+
println(s" $name [label=\"$name $op\"];")
89+
println(s" $lhs -> $name;")
90+
println(s" $rhs -> $name;")
91+
}
92+
}
93+
println("}")
94+
}
95+
6696
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day24.txt")).mkString.trim
6797

6898
def main(args: Array[String]): Unit = {
69-
println(getZValue(parseCircuit(input)))
99+
val circuit = parseCircuit(input)
100+
println(getZValue(circuit))
101+
val circuit2 = swap(swap(swap(swap(circuit, "z21", "nhn"), "tvb", "khg"), "z33", "gst"), "z12", "vdc")
102+
printCircuitDot(circuit2)
103+
println(getZValue(circuit2))
104+
println("51401618891888")
105+
106+
val circuit3 = changeInput(changeInput(circuit2, "x", 0), "asdasd", 0)
107+
println(getZValue(circuit3))
108+
109+
println(Seq("z21", "nhn", "tvb", "khg", "z33", "gst", "z12", "vdc").sorted.mkString(","))
110+
// part 2: gst,khg,nhn,tvb,vdc,z12,z21,z33 - correct
70111
}
71112
}

0 commit comments

Comments
 (0)