@@ -2,8 +2,8 @@ package AdventOfCode2022
2
2
3
3
object Day23 :
4
4
val adjacent = Seq (Point (- 1 , - 1 ), Point (0 , - 1 ), Point (1 , - 1 ), Point (- 1 , 0 ), Point (1 , 0 ), Point (- 1 , 1 ), Point (0 , 1 ), Point (1 , 1 ))
5
- val (north, south, west, east) = (Point (0 , - 1 ), Point (0 , 1 ), Point (- 1 , 0 ), Point (1 , 0 ))
6
- val order = Seq (north, south, west, east)
5
+ val order = Seq (Point (0 , - 1 ), Point (0 , 1 ), Point (- 1 , 0 ), Point (1 , 0 ))
6
+ val Seq (north, south, west, east) = order
7
7
8
8
case class Point (x : Int , y : Int ):
9
9
def + (other : Point ): Point = Point (x + other.x, y + other.y)
@@ -19,20 +19,19 @@ object Day23:
19
19
20
20
val proposals = elves.map(elf => elf -> propose(elves, moves, elf))
21
21
val occurrences = proposals.toSeq.flatMap(_._2).groupMapReduce(identity)(_ => 1 )(_ + _)
22
- val valid = occurrences.filter((_, total) => total == 1 ).keySet
23
22
val next = proposals.map { (elf, proposal) =>
24
- proposal.map(move => if valid.contains (move) then move else elf).getOrElse(elf)
23
+ proposal.map(move => if occurrences (move) == 1 then move else elf).getOrElse(elf)
25
24
}
26
25
27
26
State (next, moves.tail :+ moves.head, elves == next)
28
27
end step
29
28
30
- def propose (elves : Set [Point ], order : Seq [Point ], elf : Point ): Option [Point ] =
29
+ def propose (elves : Set [Point ], moves : Seq [Point ], elf : Point ): Option [Point ] =
31
30
val checks = adjacent.map(_ + elf).map(elves.contains)
32
31
val Seq (nw, n, ne, w, e, sw, s, se) = checks
33
32
34
33
if checks.exists(identity) then
35
- order .find {
34
+ moves .find {
36
35
case `north` => ! (nw || n || ne)
37
36
case `south` => ! (sw || s || se)
38
37
case `west` => ! (nw || w || sw)
@@ -46,10 +45,8 @@ object Day23:
46
45
val start = State (parse(input), order, false )
47
46
val elves = Iterator .iterate(start)(step).drop(10 ).next.elves
48
47
49
- val minX = elves.map(_.x).min
50
- val maxX = elves.map(_.x).max
51
- val minY = elves.map(_.y).min
52
- val maxY = elves.map(_.y).max
48
+ val (minX, maxX) = (elves.map(_.x).min, elves.map(_.x).max)
49
+ val (minY, maxY) = (elves.map(_.y).min, elves.map(_.y).max)
53
50
54
51
val points = for x <- minX to maxX; y <- minY to maxY yield Point (x, y)
55
52
points.filterNot(elves.contains).size
0 commit comments