@@ -20,7 +20,8 @@ import mouse.all.booleanSyntaxMouse
20
20
import scala .annotation .tailrec
21
21
22
22
object Advent24 extends IOApp .Simple {
23
- type Input = (Map [Wire , Boolean ], Connections )
23
+ private type Values = Map [Wire , Boolean ]
24
+ type Input = (Values , Connections )
24
25
25
26
sealed trait Wire extends Product with Serializable
26
27
private object Wire {
@@ -36,9 +37,6 @@ object Advent24 extends IOApp.Simple {
36
37
final case class Middle (a : Char , b : Char , c : Char ) extends Wire {
37
38
override def toString : String = s " $a$b$c"
38
39
}
39
- final case class Debug (s : String ) extends Wire {
40
- override def toString : String = s
41
- }
42
40
43
41
def parse (s : String ): Wire = s match {
44
42
case s " x $i" => X (i.toInt)
@@ -47,7 +45,7 @@ object Advent24 extends IOApp.Simple {
47
45
case s =>
48
46
s.toList match {
49
47
case List (a, b, c) => Middle (a, b, c)
50
- case _ => Debug (s)
48
+ case _ => s " Failed to parse $s " .fail
51
49
}
52
50
}
53
51
}
@@ -77,15 +75,17 @@ object Advent24 extends IOApp.Simple {
77
75
}
78
76
79
77
final case class Connections private (map : Map [Wire , Connection ]) {
80
- val allWires : Set [Wire ] = map.flatMap { case (k, v) => Set (k, v.a, v.b) }.toSet
78
+ val allWires : Set [Wire ] = map.flatMap { case (k, v) =>
79
+ Set (k, v.a, v.b)
80
+ }.toSet
81
81
val allOutputs : Set [Wire ] = map.keySet
82
82
83
83
def foreach (f : Connection => Unit ): Unit = map.values foreach f
84
84
85
85
// TODO: This doesn't do a sufficient test, as these bit-by-bit tests don't catch all issues that could happen. Consider adding random numbers.
86
86
private def errorsOnAddition : Int = {
87
87
def errorsAddingBit (bit : Int ): Int = {
88
- def zeroWires : Map [ Wire , Boolean ] =
88
+ def zeroWires : Values =
89
89
(0 until InputBits ).flatMap { b =>
90
90
List (
91
91
Wire .X (b) -> false ,
@@ -132,13 +132,13 @@ object Advent24 extends IOApp.Simple {
132
132
Set (c.a -> out, c.b -> out)
133
133
}
134
134
135
- val graph = GraphAlgorithms .createAdjacencyMapDirected(edges.toSeq )
135
+ val graph = GraphAlgorithms .createAdjacencyMapDirected(edges)
136
136
GraphAlgorithms .topologicalSort(graph)
137
137
}
138
138
139
139
def propagate (
140
- wires : Map [ Wire , Boolean ]
141
- ): Option [Map [ Wire , Boolean ] ] =
140
+ wires : Values
141
+ ): Option [Values ] =
142
142
topologicallySortedWires map { sorted =>
143
143
sorted.foldLeft(wires) { case (values, wire) =>
144
144
if (values.contains(wire)) {
@@ -172,13 +172,13 @@ object Advent24 extends IOApp.Simple {
172
172
if (currentScore == 0 ) {
173
173
(current, currentSwaps)
174
174
} else {
175
+ // TODO: Try to apply Genetic Algorithm or similar...
175
176
// TODO: Have a wider set of swaps to pick from!
176
177
val swaps = Set (
177
178
SetOfTwo (" hbk" , " z14" ),
178
179
SetOfTwo (" kvn" , " z18" ),
179
180
SetOfTwo (" dbb" , " z23" ),
180
181
SetOfTwo (" cvh" , " tfn" ),
181
- // SetOfTwo("z13", "z12"), // This one just to mess things up
182
182
)
183
183
val candidates = swaps.flatMap(_.toSet).map(Wire .parse).toIndexedSeq
184
184
// val candidates = current.allOutputs
@@ -210,7 +210,7 @@ object Advent24 extends IOApp.Simple {
210
210
}
211
211
212
212
final case class Connection (a : Wire , b : Wire , op : Operation ) {
213
- def result (values : Map [ Wire , Boolean ] ): Boolean = {
213
+ def result (values : Values ): Boolean = {
214
214
val aV = values.getOrElse(a, false )
215
215
val bV = values.getOrElse(b, false )
216
216
op match {
@@ -237,7 +237,7 @@ object Advent24 extends IOApp.Simple {
237
237
}
238
238
239
239
private object Connection {
240
- private val RegEx = " (\\ w+) (\\ w+) (\\ w+) -> (\\ w+)" .r
240
+ private val RegEx = " (\\ w+) (\\ w+) (\\ w+) -> (\\ w+)" .r
241
241
def parse (s : String ): (Connection , Wire ) =
242
242
s match {
243
243
case RegEx (a, op, b, out) =>
@@ -257,7 +257,7 @@ object Advent24 extends IOApp.Simple {
257
257
Wire .parse(highest),
258
258
operation,
259
259
)
260
- val output = Wire .parse(out)
260
+ val output = Wire .parse(out)
261
261
(connection, output)
262
262
case _ => s.failedToParse
263
263
}
@@ -331,8 +331,10 @@ object Advent24 extends IOApp.Simple {
331
331
def fileName (suffix : String ): String =
332
332
s " 2024/24 $suffix.txt "
333
333
334
+ private val DebugWrite = false
334
335
override def run : IO [Unit ] = for {
335
336
(wires, connections) <- IO (parseFile(fileName(" " )))
337
+ _ <- DebugWrite .whenA(debugWrite(connections))
336
338
_ <- IO .println(s " Part 1: ${part1((wires, connections))}" )
337
339
_ <- IO .println(s " Part 2: ${part2((wires, connections))}" )
338
340
} yield ()
0 commit comments