This commit is contained in:
Aly 2018-12-02 21:51:15 -08:00
parent d07dd395e1
commit d90152d660
No known key found for this signature in database
GPG key ID: 555B7346639DDAC3

View file

@ -12,6 +12,7 @@ object Day03 extends Day {
def idParser[_: P]: P[Int] = P("#" ~ numParser) def idParser[_: P]: P[Int] = P("#" ~ numParser)
def coordParser[_: P]: P[(Int, Int)] = P(numParser ~ "," ~ numParser) def coordParser[_: P]: P[(Int, Int)] = P(numParser ~ "," ~ numParser)
def dimensParser[_: P]: P[(Int, Int)] = P(numParser ~ "x" ~ numParser) def dimensParser[_: P]: P[(Int, Int)] = P(numParser ~ "x" ~ numParser)
def claimParser[_: P]: P[Claim] = P(idParser ~ "@" ~ coordParser ~ ":" ~ dimensParser).map { def claimParser[_: P]: P[Claim] = P(idParser ~ "@" ~ coordParser ~ ":" ~ dimensParser).map {
case (id, coords, dimens) => Claim(id, coords, dimens) case (id, coords, dimens) => Claim(id, coords, dimens)
} }
@ -26,15 +27,15 @@ object Day03 extends Day {
(c, p, s) match { (c, p, s) match {
case ((cx, cy), (px, py), (sw, sh)) => case ((cx, cy), (px, py), (sw, sh)) =>
cx >= px && cx >= px &&
cx < px + sw && cx < px + sw &&
cy >= py && cy >= py &&
cy < py + sh cy < py + sh
} }
} }
def mapSection[A](l: List[List[A]], p: (Int, Int), s: (Int, Int), f: A => A): List[List[A]] = { def mapSection[A](l: List[List[A]], p: (Int, Int), s: (Int, Int), f: A => A): List[List[A]] = {
val coordinated = l.zipWithIndex.map { case (n, i) => n.zipWithIndex.map { case (e, y) => (e, y, i) } } val coordinated = l.zipWithIndex.map { case (n, i) => n.zipWithIndex.map { case (e, y) => (e, y, i) } }
val mapped = coordinated.map(_.map { case (e, y, x) => if(insideRange((x, y), p, s)) f(e) else e }) val mapped = coordinated.map(_.map { case (e, y, x) => if (insideRange((x, y), p, s)) f(e) else e })
mapped mapped
} }