Skip to content

Instantly share code, notes, and snippets.

@JavadocMD
Last active December 14, 2022 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JavadocMD/9d5ce303c9e2a2ec9129f35a00d5b644 to your computer and use it in GitHub Desktop.
Save JavadocMD/9d5ce303c9e2a2ec9129f35a00d5b644 to your computer and use it in GitHub Desktop.
A solution to Advent of Code 2022 Day 1 using Akka Actors.
/*
Copyright 2022 Tyler Coles (javadocmd.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import akka.NotUsed
import akka.actor.typed.ActorRef
import akka.actor.typed.ActorSystem
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import scala.io.Source
/*
* Solution for Day 1 using Akka Actors.
*
* Assumes we have the puzzle input in a resource file called "Day01.input.txt".
*
* Three actors are involved: ReadLines -> GroupSum -> TopThree
* - ReadLines reads each line of the input.
* - GroupSum computes a running sum for a group of lines, and emits the sum when encountering a blank.
* - TopThree keeps track of the best three sums.
*
* Command and control is performed by Main, which spawns and connects the actors and kicks off
* processing by passing the `File` message to ReadLines. Termination is accomplished by having
* ReadLines issue a `Done` message at the end of the file, which is then relayed by GroupSum to
* TopThree. Finally TopThree emits `Solution` back to Main which computes the final answer for
* both parts and prints them out.
*/
object Day01AkkaActors:
final def main(args: Array[String]): Unit = ActorSystem(Main(), "Main")
object Main:
sealed trait Message
case class Solution(top: (Int, Int, Int)) extends Message
def apply(): Behavior[Main.Message] = Behaviors.setup { context =>
val topThree = context.spawn(TopThree(context.self), "TopThree")
val groupSum = context.spawn(GroupSum(topThree), "GroupSum")
val readLines = context.spawn(ReadLines(groupSum), "ReadLines")
readLines ! ReadLines.File("Day01.input.txt")
Behaviors.receiveMessage { case Solution((a, b, c)) =>
println(a)
println(a + b + c)
Behaviors.stopped
}
}
end Main
object ReadLines:
sealed trait Message
case class File(name: String) extends Message
def apply(target: ActorRef[GroupSum.Message]): Behavior[ReadLines.Message] = Behaviors.receiveMessage {
case File(name) =>
val source = Source.fromResource(name)
try {
for x <- source.getLines do target ! GroupSum.Line(x)
} finally source.close()
target ! GroupSum.Done
Behaviors.same
}
end ReadLines
object GroupSum:
sealed trait Message
case class Line(x: String) extends Message
case object Done extends Message
def apply(target: ActorRef[TopThree.Message]): Behavior[GroupSum.Message] = Behaviors.setup { context =>
var groupSum = 0
Behaviors.receiveMessage {
case Line("") =>
target ! TopThree.Sum(groupSum)
groupSum = 0
Behaviors.same
case Line(x) =>
groupSum += Integer.parseInt(x)
Behaviors.same
case Done =>
target ! TopThree.Sum(groupSum)
target ! TopThree.Done
Behaviors.same
}
}
end GroupSum
object TopThree:
sealed trait Message
case class Sum(x: Int) extends Message
case object Done extends Message
def apply(target: ActorRef[Main.Message]): Behavior[TopThree.Message] = Behaviors.setup { context =>
var topThree = Vector(0, 0, 0)
Behaviors.receiveMessage {
case Sum(x) =>
topThree = topThree.appended(x).sorted(Ordering[Int].reverse).take(3)
Behaviors.same
case Done =>
val Vector(a, b, c) = topThree
target ! Main.Solution((a, b, c))
topThree = Vector(0, 0, 0)
Behaviors.same
}
}
end TopThree
@JavadocMD
Copy link
Author

Scala 3.2.1 with dependency com.typesafe.akka::akka-actor-typed:2.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment