Skip to content

Commit

Permalink
Solve 2023 day 20 part 2 semi-manually
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 20, 2023
1 parent f6c1689 commit 619665a
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/main/scala/eu/sim642/adventofcode2023/Day20.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@ object Day20 {
lowPulses * highPulses
}

def countRx(circuit: Circuit): Long = {
val mCircuit = circuit.to(mutable.Map)

var presses = 0L

while (true) {

val queue = mutable.Queue.empty[(String, Pulse, String)]
queue.enqueue(("button", Pulse.Low, "broadcaster"))
presses += 1

while (queue.nonEmpty) {
val s@(from, pulse, to) = queue.dequeue()
//println(s)

s match {
case (_, Pulse.Low, "rx") =>
return presses
case (from, Pulse.High, "sq") =>
println(s"high from $from after $presses")
case _ => ()
}

mCircuit.get(to) match {
case None => () // output
case Some(module, outputs) =>
val (newModule, outPulse) = module.handle(from, pulse)
mCircuit(to) = (newModule, outputs)

for {
pulse <- outPulse
output <- outputs
} queue.enqueue((to, pulse, output))
}
}
}

???
}


def parseModule(s: String): (String, (Module, Seq[String])) = s match {
case s"$moduleStr -> $outputsStr" =>
Expand Down Expand Up @@ -102,9 +142,20 @@ object Day20 {

def parseCircuit(input: String): Circuit = initializeConjunctions(input.linesIterator.map(parseModule).toMap)

def printDot(circuit: Circuit): Unit = {
println("digraph G {")
for ((module, (m, outputs)) <- circuit) {
for (output <- outputs)
println(s"$module -> $output;")
}
println("}")
}

lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day20.txt")).mkString.trim

def main(args: Array[String]): Unit = {
println(countPulses(parseCircuit(input)))
//println(countPulses(parseCircuit(input)))
//printDot(parseCircuit(input))
println(countRx(parseCircuit(input))) // 217317393039529
}
}

0 comments on commit 619665a

Please sign in to comment.