Skip to content

Commit

Permalink
refactor: shorter size.contains
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 8, 2024
1 parent 62968c8 commit 61020e0
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions 2024/day08.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ extension (a: Pos)
val delta = (b - a)
Iterator.iterate(a + delta)(_ + delta)

case class Context(size: Size, antennaes: Map[Char, Vector[Pos]]):
case class Context(size: Size, antennas: Map[Char, Vector[Pos]]):
def solve(fn: ((Pos, Pos)) => IterableOnce[Pos]) =
antennaes.values.flatMap(_.combinationsN(2).flatMap(fn)).toSet
antennas.values.flatMap(_.combinationsN(2).flatMap(fn)).toSet

lazy val part1 =
solve((a, b) => Vector(a + (a - b), b + (b - a))).filter(size.contains).size
solve((a, b) => Vector(a + (a - b), b + (b - a))).filter(size(_)).size
lazy val part2 = solve((a, b) =>
(a deltas b).takeWhile(size.contains)
++ (b deltas a).takeWhile(size.contains),
(a deltas b).takeWhile(size(_)) ++ (b deltas a).takeWhile(size(_)),
).size

object Context:
Expand All @@ -24,13 +23,13 @@ object Context:

def apply(grid: Array[Array[Char]]): Context =
val size = Size(grid)
val antennaes = (for
val antennas = (for
y <- 0 until size.height
x <- 0 until size.width
c = grid(y)(x)
if c != '.' && c != '#'
yield c -> Pos(x, y)).toVector.groupMap(_._1)(_._2)
Context(size, antennaes)
Context(size, antennas)

@main def main() =
val input = readInput(this).mkString
Expand Down

0 comments on commit 61020e0

Please sign in to comment.