From 05b19a527a8af0b76265289b9d738243c869a6a5 Mon Sep 17 00:00:00 2001 From: nmcb Date: Tue, 24 Feb 2026 08:21:29 +0100 Subject: [PATCH] cleanup --- 2021/src/main/scala/aoc2021/Day05.scala | 49 ++++++++++--------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/2021/src/main/scala/aoc2021/Day05.scala b/2021/src/main/scala/aoc2021/Day05.scala index 6b44320..4056b63 100644 --- a/2021/src/main/scala/aoc2021/Day05.scala +++ b/2021/src/main/scala/aoc2021/Day05.scala @@ -1,6 +1,7 @@ package aoc2021 import nmcb.* +import nmcb.predef.* import scala.collection.* import scala.collection.immutable.{Map, Set} @@ -8,40 +9,27 @@ import scala.util.matching.Regex object Day05 extends AoC: - val LineLit: Regex = """(\d+),(\d+) -> (\d+),(\d+)""".r + val Line: Regex = """(\d+),(\d+) -> (\d+),(\d+)""".r val puzzle: Vector[Line] = - lines - .map: - case LineLit(x0, y0, x1, y1) if y0 == y1 => - HLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) - case LineLit(x0, y0, x1, y1) if x0 == x1 => - VLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) - case LineLit(x0, y0, x1, y1) => - DLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) + lines.collect: + case Line(x0, y0, x1, y1) if y0 == y1 => HLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) + case Line(x0, y0, x1, y1) if x0 == x1 => VLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) + case Line(x0, y0, x1, y1) => DLine(x0.toInt, y0.toInt, x1.toInt, y1.toInt) sealed trait Line: - val points: Set[(Int,Int)] + val points: Set[(Int, Int)] case class HLine(x0: Int, y0: Int, x1: Int, y1: Int) extends Line: - - val points: Set[(Int,Int)] = - if x0 < x1 then - (x0 to x1).map((_,y0)).toSet - else - (x1 to x0).map((_,y0)).toSet + val points: Set[(Int, Int)] = + if x0 < x1 then (x0 to x1).map((_, y0)).toSet else (x1 to x0).map((_, y0)).toSet case class VLine(x0: Int, y0: Int, x1: Int, y1: Int) extends Line: - - val points: Set[(Int,Int)] = - if y0 < y1 then - (y0 to y1).map((x0, _)).toSet - else - (y1 to y0).map((x0, _)).toSet + val points: Set[(Int, Int)] = + if y0 < y1 then (y0 to y1).map((x0, _)).toSet else (y1 to y0).map((x0, _)).toSet case class DLine(x0: Int, y0: Int, x1: Int, y1: Int) extends Line: - - val points: Set[(Int,Int)] = + val points: Set[(Int, Int)] = (0 to (math.max(x0, x1) - math.min(x0, x1))) .map: n => if x0 < x1 then @@ -56,16 +44,17 @@ object Day05 extends AoC: (x0 - n, y0 - n) .toSet - case class Floor(locations: Map[(Int,Int), Int]): - + case class Floor(locations: Map[(Int, Int), Int]): val crossings: Int = - locations.count((_,nr) => nr >= 2) + locations.count(_.right >= 2) object Floor: - def apply(lines: Vector[Line]): Floor = - val locations = mutable.Map.empty[(Int,Int),Int] - for l <- lines ; p <- l.points do + val locations = mutable.Map.empty[(Int, Int), Int] + for + l <- lines + p <- l.points + do locations.updateWith(p): case None => Some(1) case Some(nr) => Some(nr + 1)