Skip to content

Commit

Permalink
Solve 2024 day 14 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sim642 committed Dec 14, 2024
1 parent 0f5e379 commit fcb53f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/scala/eu/sim642/adventofcode2024/Day14.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ object Day14 {
quadrants((1, 1)) * quadrants((-1, 1)) * quadrants((1, -1)) * quadrants((-1, -1))
}

def findEasterEgg(robots: Seq[Robot], roomSize: Pos = Pos(101, 103)): Int = {
Iterator.from(0)
.map(t => robots.map(_.step(t, roomSize))) // TODO: optimize
.map(newRobots => newRobots.groupCount(_.pos))
.indexWhere(_.values.forall(_ <= 1)) // look for arrangement with unique positions
}

def parseRobot(s: String): Robot = s match {
case s"p=$pX,$pY v=$vX,$vY" =>
Robot(Pos(pX.toInt, pY.toInt), Pos(vX.toInt, vY.toInt))
Expand All @@ -37,5 +44,6 @@ object Day14 {

def main(args: Array[String]): Unit = {
println(safetyFactor(parseRobots(input)))
println(findEasterEgg(parseRobots(input)))
}
}
4 changes: 4 additions & 0 deletions src/test/scala/eu/sim642/adventofcode2024/Day14Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ class Day14Test extends AnyFunSuite {
test("Part 1 input answer") {
assert(safetyFactor(parseRobots(input)) == 228690000)
}

test("Part 2 input answer") {
assert(findEasterEgg(parseRobots(input)) == 7093)
}
}

0 comments on commit fcb53f4

Please sign in to comment.