-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.scala
24 lines (18 loc) · 898 Bytes
/
day6.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
import scala.io.Source
object Day6 {
def main(args: Array[String]): Unit = {
val orbits = Source.fromFile("./day6.input").getLines.toList.map(_.split(')')).map((a) => (a(0),a(1)))
// First
def countOrbits (orbits: List[(String, String)], current: Int)(orbit: String): Int = {
val toCount = orbits.filter(_._1 == orbit).map(_._2)
current + toCount.map(countOrbits(orbits, current + 1)).sum
}
println(countOrbits(orbits, 0)("COM"))
// Second
def traverseOrbits (orbits: List[(String, String)], current: Int, visited: Set[String])(orbit: String): Int = {
val toCount = {orbits.filter(_._2 == orbit).map(_._1) ++ orbits.filter(_._1 == orbit).map(_._2)}.filterNot(visited)
if (orbit == "SAN") current-1 else toCount.map(traverseOrbits(orbits, current + 1, visited + orbit)).sum
}
println(traverseOrbits(orbits, -1, Set())("YOU"))
}
}