Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions 2021/src/main/scala/aoc2021/Day05.scala
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
package aoc2021

import nmcb.*
import nmcb.predef.*

import scala.collection.*
import scala.collection.immutable.{Map, Set}
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
Expand All @@ -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)
Expand Down