From 61020e02f4a924168a49dfb115de5f2a28a69e43 Mon Sep 17 00:00:00 2001 From: scarf Date: Sun, 8 Dec 2024 22:27:04 +0900 Subject: [PATCH] refactor: shorter `size.contains` --- 2024/day08.scala | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/2024/day08.scala b/2024/day08.scala index 7031066..6b242aa 100644 --- a/2024/day08.scala +++ b/2024/day08.scala @@ -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: @@ -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