Skip to content

Commit 764a40c

Browse files
committed
2024-24 Refactoring
1 parent ed24e39 commit 764a40c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

scala2/src/main/scala/jurisk/adventofcode/y2024/Advent24.scala

+16-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import mouse.all.booleanSyntaxMouse
2020
import scala.annotation.tailrec
2121

2222
object Advent24 extends IOApp.Simple {
23-
type Input = (Map[Wire, Boolean], Connections)
23+
private type Values = Map[Wire, Boolean]
24+
type Input = (Values, Connections)
2425

2526
sealed trait Wire extends Product with Serializable
2627
private object Wire {
@@ -36,9 +37,6 @@ object Advent24 extends IOApp.Simple {
3637
final case class Middle(a: Char, b: Char, c: Char) extends Wire {
3738
override def toString: String = s"$a$b$c"
3839
}
39-
final case class Debug(s: String) extends Wire {
40-
override def toString: String = s
41-
}
4240

4341
def parse(s: String): Wire = s match {
4442
case s"x$i" => X(i.toInt)
@@ -47,7 +45,7 @@ object Advent24 extends IOApp.Simple {
4745
case s =>
4846
s.toList match {
4947
case List(a, b, c) => Middle(a, b, c)
50-
case _ => Debug(s)
48+
case _ => s"Failed to parse $s".fail
5149
}
5250
}
5351
}
@@ -77,15 +75,17 @@ object Advent24 extends IOApp.Simple {
7775
}
7876

7977
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
8181
val allOutputs: Set[Wire] = map.keySet
8282

8383
def foreach(f: Connection => Unit): Unit = map.values foreach f
8484

8585
// 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.
8686
private def errorsOnAddition: Int = {
8787
def errorsAddingBit(bit: Int): Int = {
88-
def zeroWires: Map[Wire, Boolean] =
88+
def zeroWires: Values =
8989
(0 until InputBits).flatMap { b =>
9090
List(
9191
Wire.X(b) -> false,
@@ -132,13 +132,13 @@ object Advent24 extends IOApp.Simple {
132132
Set(c.a -> out, c.b -> out)
133133
}
134134

135-
val graph = GraphAlgorithms.createAdjacencyMapDirected(edges.toSeq)
135+
val graph = GraphAlgorithms.createAdjacencyMapDirected(edges)
136136
GraphAlgorithms.topologicalSort(graph)
137137
}
138138

139139
def propagate(
140-
wires: Map[Wire, Boolean]
141-
): Option[Map[Wire, Boolean]] =
140+
wires: Values
141+
): Option[Values] =
142142
topologicallySortedWires map { sorted =>
143143
sorted.foldLeft(wires) { case (values, wire) =>
144144
if (values.contains(wire)) {
@@ -172,13 +172,13 @@ object Advent24 extends IOApp.Simple {
172172
if (currentScore == 0) {
173173
(current, currentSwaps)
174174
} else {
175+
// TODO: Try to apply Genetic Algorithm or similar...
175176
// TODO: Have a wider set of swaps to pick from!
176177
val swaps = Set(
177178
SetOfTwo("hbk", "z14"),
178179
SetOfTwo("kvn", "z18"),
179180
SetOfTwo("dbb", "z23"),
180181
SetOfTwo("cvh", "tfn"),
181-
// SetOfTwo("z13", "z12"), // This one just to mess things up
182182
)
183183
val candidates = swaps.flatMap(_.toSet).map(Wire.parse).toIndexedSeq
184184
// val candidates = current.allOutputs
@@ -210,7 +210,7 @@ object Advent24 extends IOApp.Simple {
210210
}
211211

212212
final case class Connection(a: Wire, b: Wire, op: Operation) {
213-
def result(values: Map[Wire, Boolean]): Boolean = {
213+
def result(values: Values): Boolean = {
214214
val aV = values.getOrElse(a, false)
215215
val bV = values.getOrElse(b, false)
216216
op match {
@@ -237,7 +237,7 @@ object Advent24 extends IOApp.Simple {
237237
}
238238

239239
private object Connection {
240-
private val RegEx = "(\\w+) (\\w+) (\\w+) -> (\\w+)".r
240+
private val RegEx = "(\\w+) (\\w+) (\\w+) -> (\\w+)".r
241241
def parse(s: String): (Connection, Wire) =
242242
s match {
243243
case RegEx(a, op, b, out) =>
@@ -257,7 +257,7 @@ object Advent24 extends IOApp.Simple {
257257
Wire.parse(highest),
258258
operation,
259259
)
260-
val output = Wire.parse(out)
260+
val output = Wire.parse(out)
261261
(connection, output)
262262
case _ => s.failedToParse
263263
}
@@ -331,8 +331,10 @@ object Advent24 extends IOApp.Simple {
331331
def fileName(suffix: String): String =
332332
s"2024/24$suffix.txt"
333333

334+
private val DebugWrite = false
334335
override def run: IO[Unit] = for {
335336
(wires, connections) <- IO(parseFile(fileName("")))
337+
_ <- DebugWrite.whenA(debugWrite(connections))
336338
_ <- IO.println(s"Part 1: ${part1((wires, connections))}")
337339
_ <- IO.println(s"Part 2: ${part2((wires, connections))}")
338340
} yield ()

0 commit comments

Comments
 (0)