Skip to content

Commit 9b2acce

Browse files
committed
Clean up 2024 day 14
1 parent fcb53f4 commit 9b2acce

File tree

2 files changed

+145
-14
lines changed

2 files changed

+145
-14
lines changed

src/main/scala/eu/sim642/adventofcode2024/Day14.scala

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,44 @@ object Day14 {
99
// copied from 2021 day 25
1010
extension (pos: Pos) {
1111
def %+(other: Pos): Pos = Pos(pos.x %+ other.x, pos.y %+ other.y)
12+
infix def compareTo(other: Pos): Pos = Pos(pos.x `compareTo` other.x, pos.y `compareTo` other.y)
13+
def sign: Pos = Pos(pos.x.sign, pos.y.sign)
1214
}
1315

1416
case class Robot(pos: Pos, velocity: Pos) {
17+
def step(roomSize: Pos): Robot = Robot((pos + velocity) %+ roomSize, velocity)
1518
def step(t: Int, roomSize: Pos): Robot = Robot((pos + t *: velocity) %+ roomSize, velocity)
1619
}
1720

18-
def safetyFactor(robots: Seq[Robot], roomSize: Pos = Pos(101, 103)): Int = {
21+
val inputRoomSize: Pos = Pos(101, 103)
22+
23+
def safetyFactor(robots: Seq[Robot], roomSize: Pos = inputRoomSize): Int = {
1924
val newRobots = robots.map(_.step(100, roomSize))
20-
//newRobots.foreach(println)
2125
val roomMiddle = Pos(roomSize.x / 2, roomSize.y / 2)
22-
//println(roomMiddle)
23-
//val quadrants0 = newRobots.groupBy(robot => (robot.pos.x.compare(roomMiddle.x), robot.pos.y.compare(roomMiddle.y))).withDefaultValue(0)
24-
//quadrants0.foreach(println)
25-
val quadrants = newRobots.groupCount(robot => (robot.pos.x.compare(roomMiddle.x), robot.pos.y.compare(roomMiddle.y))).withDefaultValue(0)
26-
quadrants((1, 1)) * quadrants((-1, 1)) * quadrants((1, -1)) * quadrants((-1, -1))
26+
val quadrants = newRobots.groupCount(robot =>
27+
(robot.pos `compareTo` roomMiddle).sign // use .sign to clamp to -1, 0, 1
28+
).withDefaultValue(0)
29+
Pos.diagonalOffsets.map(quadrants).product
2730
}
2831

29-
def findEasterEgg(robots: Seq[Robot], roomSize: Pos = Pos(101, 103)): Int = {
30-
Iterator.from(0)
31-
.map(t => robots.map(_.step(t, roomSize))) // TODO: optimize
32-
.map(newRobots => newRobots.groupCount(_.pos))
33-
.indexWhere(_.values.forall(_ <= 1)) // look for arrangement with unique positions
32+
def findEasterEgg(robots: Seq[Robot], roomSize: Pos = inputRoomSize): (Seq[Robot], Int) = {
33+
Iterator.iterate(robots)(_.map(_.step(roomSize)))
34+
.zipWithIndex
35+
.find(_._1.groupCount(_.pos).values.forall(_ <= 1)) // look for arrangement with unique positions
36+
// TODO: optimize by stopping groupCount when some goes over 1
37+
.get
38+
}
39+
40+
def printRobots(robots: Seq[Robot], roomSize: Pos = inputRoomSize): Unit = {
41+
val posRobots = robots.groupCount(_.pos)
42+
43+
for (y <- 0 until roomSize.y) {
44+
for (x <- 0 until roomSize.x) {
45+
val pos = Pos(x, y)
46+
print(posRobots.get(pos).map(count => ('0' + count).toChar).getOrElse('.')) // TODO: toDigit?
47+
}
48+
println()
49+
}
3450
}
3551

3652
def parseRobot(s: String): Robot = s match {
@@ -44,6 +60,9 @@ object Day14 {
4460

4561
def main(args: Array[String]): Unit = {
4662
println(safetyFactor(parseRobots(input)))
47-
println(findEasterEgg(parseRobots(input)))
63+
64+
val (easterEggRobots, easterEggTime) = findEasterEgg(parseRobots(input))
65+
printRobots(easterEggRobots)
66+
println(easterEggTime)
4867
}
4968
}

src/test/scala/eu/sim642/adventofcode2024/Day14Test.scala

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Day14.*
44
import eu.sim642.adventofcodelib.pos.Pos
55
import org.scalatest.funsuite.AnyFunSuite
66

7+
import java.io.ByteArrayOutputStream
8+
79
class Day14Test extends AnyFunSuite {
810

911
val exampleInput =
@@ -29,6 +31,116 @@ class Day14Test extends AnyFunSuite {
2931
}
3032

3133
test("Part 2 input answer") {
32-
assert(findEasterEgg(parseRobots(input)) == 7093)
34+
val (easterEggRobots, easterEggTime) = findEasterEgg(parseRobots(input))
35+
assert(easterEggTime == 7093)
36+
37+
val out = new ByteArrayOutputStream()
38+
Console.withOut(out) {
39+
printRobots(easterEggRobots)
40+
}
41+
assert(out.toString.trim ==
42+
""".........................1...........................................................................
43+
|..............................1......................................................................
44+
|.....................................................................................1.........1.....
45+
|..............................................1.........................................1............
46+
|.....................................................................................................
47+
|.1.............1.....................................................................................
48+
|.....................................................................................................
49+
|......1.....1.......................................................1...................1............
50+
|1.........................................................................1..........................
51+
|..........................................1..........................................................
52+
|.....................................................................................................
53+
|..............................................................................1................1.....
54+
|................................1...1...............1..............................1.................
55+
|....................1................................................................................
56+
|.......................1........................................................1....................
57+
|...........1...........1.....................1.......................................................
58+
|...................................................1..........1......................................
59+
|..................1...........1...................................................................1..
60+
|.....................................................................................................
61+
|.................1...........................1.......................................................
62+
|................1..1................................1................................................
63+
|.....................................................................................................
64+
|.....................................................................................................
65+
|............................1.1.....1111111111111111111111111111111..................................
66+
|.1..................................1.............................1..................................
67+
|....................................1.............................1..................................
68+
|...1................................1.............................1..........................1.......
69+
|..........................1.........1.............................1...................1..............
70+
|....................................1..............1..............1..................................
71+
|...............................1....1.............111.............1..................................
72+
|....................................1............11111............1..................................
73+
|....................................1...........1111111...........1......................1........1..
74+
|....................................1..........111111111..........1............1................1....
75+
|....................................1............11111............1........1.........................
76+
|....................................1...........1111111...........1..................................
77+
|.....................1............1.1..........111111111..........1.1............................1...
78+
|....................................1.........11111111111.........1..................................
79+
|....................................1........1111111111111........1..................................
80+
|....................................1..........111111111..........1.1..................11............
81+
|....................................1.........11111111111.........1..................................
82+
|....................................1........1111111111111........1..................................
83+
|......................1.............1.......111111111111111.......1..................................
84+
|.......1............................1......11111111111111111......1..................................
85+
|...........1..1.....................1........1111111111111........1................1.................
86+
|....................................1.......111111111111111.......1...........................1......
87+
|....................................1......11111111111111111......1.....1.................1..........
88+
|......1.............................1.....1111111111111111111.....1..........1.......................
89+
|................1...................1....111111111111111111111....1...................1..............
90+
|..................................1.1.............111.............1..................................
91+
|....................................1.............111.............1......1...........................
92+
|....................................1.............111.............1..................................
93+
|....................................1.............................1..................................
94+
|............................1.......1.............................1..................................
95+
|....................................1.............................1..................................
96+
|...............1....................1.............................1..............1...................
97+
|..............1.........1...........1111111111111111111111111111111.....................1............
98+
|...........................1........................................1................................
99+
|....................1.....1................1..........................1..............................
100+
|............................1........................................................................
101+
|...............................................................................................1.....
102+
|.........................1......................................1....................................
103+
|.........................................................................1...1.......................
104+
|............................................1........................................................
105+
|.............................................................................1........1..............
106+
|.....................................................................................................
107+
|.....................................................................................................
108+
|...1............................................1..............1.....................................
109+
|.....................................................................................................
110+
|.....................................................................................................
111+
|....................................................................................1............1...
112+
|..................................................................1..................................
113+
|................................1.......................1............................................
114+
|.....................................................................................................
115+
|......................1..............................................................................
116+
|..............................1............1.........................................................
117+
|..............................................................1......................................
118+
|................................................................1....................................
119+
|......................................1............................1....................1............
120+
|.....................................................................................................
121+
|.....................................................................................................
122+
|.........................................................................1....................1......
123+
|....................................1.......1............1.......1.........................1.........
124+
|.....................................................................................................
125+
|...................................1................................1................................
126+
|..........................................................................1..........................
127+
|.....................................................................1.........1.................1...
128+
|........................................1...........1...................1............................
129+
|.................................................................1....1..............................
130+
|...........................................1.........................................................
131+
|.....................................................................................................
132+
|.1....1............................................................................1.................
133+
|.........................................................1...........................................
134+
|..........................................................................................1..........
135+
|...................................1........................................................1........
136+
|........................1......................................1..........................1.1........
137+
|.......1..................................................................1.........................1
138+
|.....................................................................................................
139+
|.....................................................................................................
140+
|..................................................................1................................1.
141+
|.........................................................................1...........................
142+
|..........................1..........................................................................
143+
|.................................................................1...................................
144+
|...........1.........................................................................................""".stripMargin)
33145
}
34146
}

0 commit comments

Comments
 (0)