@@ -2,8 +2,8 @@ package AdventOfCode2022
22
33object Day23 :
44 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
77
88 case class Point (x : Int , y : Int ):
99 def + (other : Point ): Point = Point (x + other.x, y + other.y)
@@ -19,20 +19,19 @@ object Day23:
1919
2020 val proposals = elves.map(elf => elf -> propose(elves, moves, elf))
2121 val occurrences = proposals.toSeq.flatMap(_._2).groupMapReduce(identity)(_ => 1 )(_ + _)
22- val valid = occurrences.filter((_, total) => total == 1 ).keySet
2322 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)
2524 }
2625
2726 State (next, moves.tail :+ moves.head, elves == next)
2827 end step
2928
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 ] =
3130 val checks = adjacent.map(_ + elf).map(elves.contains)
3231 val Seq (nw, n, ne, w, e, sw, s, se) = checks
3332
3433 if checks.exists(identity) then
35- order .find {
34+ moves .find {
3635 case `north` => ! (nw || n || ne)
3736 case `south` => ! (sw || s || se)
3837 case `west` => ! (nw || w || sw)
@@ -46,10 +45,8 @@ object Day23:
4645 val start = State (parse(input), order, false )
4746 val elves = Iterator .iterate(start)(step).drop(10 ).next.elves
4847
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)
5350
5451 val points = for x <- minX to maxX; y <- minY to maxY yield Point (x, y)
5552 points.filterNot(elves.contains).size
0 commit comments