-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay25.scala
34 lines (28 loc) · 1.19 KB
/
Day25.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import scala.io.Source.stdin
@main def main(idx: Int) =
var xs = ( for { (r, y) <- stdin.getLines().zipWithIndex
(v, x) <- r.zipWithIndex
if v == '>' || v == 'v'
} yield ((y, x), v) ).toMap
val h = xs.keys.map{_._1}.max + 1
val w = xs.keys.map{_._2}.max + 1
val modifier = Map('>' -> (0, 1), 'v' -> (1, 0))
def nextCoord(coord: (Int, Int), dir: Char) =
val ((y, x), (dy, dx)) = (coord, modifier(dir))
((y + dy) % h, (x + dx) % w)
def advance(c: Char, m: Map[(Int, Int), Char]) =
val (target, other) = m.partition{ case (coord, v) =>
c == v && '.' == m.getOrElse(nextCoord(coord, v), '.')}
(!target.isEmpty, other ++ target.view.map{case (k, v) => (nextCoord(k, v), v)})
println {
Iterator.from(1).scanLeft(Option(xs)){(t, _) => t match {
case Some(xs) =>
val (modifiedRight, right) = advance('>', xs)
val (modifiedDown, down) = advance('v', right)
if modifiedRight || modifiedDown
then Some(down)
else None
case _ => None
}}.zipWithIndex.takeWhile(!_._1.isEmpty).flatMap{case (v, i) => v.map{(_, i)}}
.to(LazyList).last._2 + 1
}