Skip to content

Commit

Permalink
2024-24 Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jurisk committed Dec 25, 2024
1 parent ed24e39 commit 764a40c
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions scala2/src/main/scala/jurisk/adventofcode/y2024/Advent24.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import mouse.all.booleanSyntaxMouse
import scala.annotation.tailrec

object Advent24 extends IOApp.Simple {
type Input = (Map[Wire, Boolean], Connections)
private type Values = Map[Wire, Boolean]
type Input = (Values, Connections)

sealed trait Wire extends Product with Serializable
private object Wire {
Expand All @@ -36,9 +37,6 @@ object Advent24 extends IOApp.Simple {
final case class Middle(a: Char, b: Char, c: Char) extends Wire {
override def toString: String = s"$a$b$c"
}
final case class Debug(s: String) extends Wire {
override def toString: String = s
}

def parse(s: String): Wire = s match {
case s"x$i" => X(i.toInt)
Expand All @@ -47,7 +45,7 @@ object Advent24 extends IOApp.Simple {
case s =>
s.toList match {
case List(a, b, c) => Middle(a, b, c)
case _ => Debug(s)
case _ => s"Failed to parse $s".fail
}
}
}
Expand Down Expand Up @@ -77,15 +75,17 @@ object Advent24 extends IOApp.Simple {
}

final case class Connections private (map: Map[Wire, Connection]) {
val allWires: Set[Wire] = map.flatMap { case (k, v) => Set(k, v.a, v.b) }.toSet
val allWires: Set[Wire] = map.flatMap { case (k, v) =>
Set(k, v.a, v.b)
}.toSet
val allOutputs: Set[Wire] = map.keySet

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

// 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.
private def errorsOnAddition: Int = {
def errorsAddingBit(bit: Int): Int = {
def zeroWires: Map[Wire, Boolean] =
def zeroWires: Values =
(0 until InputBits).flatMap { b =>
List(
Wire.X(b) -> false,
Expand Down Expand Up @@ -132,13 +132,13 @@ object Advent24 extends IOApp.Simple {
Set(c.a -> out, c.b -> out)
}

val graph = GraphAlgorithms.createAdjacencyMapDirected(edges.toSeq)
val graph = GraphAlgorithms.createAdjacencyMapDirected(edges)
GraphAlgorithms.topologicalSort(graph)
}

def propagate(
wires: Map[Wire, Boolean]
): Option[Map[Wire, Boolean]] =
wires: Values
): Option[Values] =
topologicallySortedWires map { sorted =>
sorted.foldLeft(wires) { case (values, wire) =>
if (values.contains(wire)) {
Expand Down Expand Up @@ -172,13 +172,13 @@ object Advent24 extends IOApp.Simple {
if (currentScore == 0) {
(current, currentSwaps)
} else {
// TODO: Try to apply Genetic Algorithm or similar...
// TODO: Have a wider set of swaps to pick from!
val swaps = Set(
SetOfTwo("hbk", "z14"),
SetOfTwo("kvn", "z18"),
SetOfTwo("dbb", "z23"),
SetOfTwo("cvh", "tfn"),
// SetOfTwo("z13", "z12"), // This one just to mess things up
)
val candidates = swaps.flatMap(_.toSet).map(Wire.parse).toIndexedSeq
// val candidates = current.allOutputs
Expand Down Expand Up @@ -210,7 +210,7 @@ object Advent24 extends IOApp.Simple {
}

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

private object Connection {
private val RegEx = "(\\w+) (\\w+) (\\w+) -> (\\w+)".r
private val RegEx = "(\\w+) (\\w+) (\\w+) -> (\\w+)".r
def parse(s: String): (Connection, Wire) =
s match {
case RegEx(a, op, b, out) =>
Expand All @@ -257,7 +257,7 @@ object Advent24 extends IOApp.Simple {
Wire.parse(highest),
operation,
)
val output = Wire.parse(out)
val output = Wire.parse(out)
(connection, output)
case _ => s.failedToParse
}
Expand Down Expand Up @@ -331,8 +331,10 @@ object Advent24 extends IOApp.Simple {
def fileName(suffix: String): String =
s"2024/24$suffix.txt"

private val DebugWrite = false
override def run: IO[Unit] = for {
(wires, connections) <- IO(parseFile(fileName("")))
_ <- DebugWrite.whenA(debugWrite(connections))
_ <- IO.println(s"Part 1: ${part1((wires, connections))}")
_ <- IO.println(s"Part 2: ${part2((wires, connections))}")
} yield ()
Expand Down

0 comments on commit 764a40c

Please sign in to comment.