Skip to content

Commit c226899

Browse files
author
Kalle Wuoti
committed
Day 9 solution
1 parent e412253 commit c226899

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

day9/input.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
AlphaCentauri to Snowdin = 66
2+
AlphaCentauri to Tambi = 28
3+
AlphaCentauri to Faerun = 60
4+
AlphaCentauri to Norrath = 34
5+
AlphaCentauri to Straylight = 34
6+
AlphaCentauri to Tristram = 3
7+
AlphaCentauri to Arbre = 108
8+
Snowdin to Tambi = 22
9+
Snowdin to Faerun = 12
10+
Snowdin to Norrath = 91
11+
Snowdin to Straylight = 121
12+
Snowdin to Tristram = 111
13+
Snowdin to Arbre = 71
14+
Tambi to Faerun = 39
15+
Tambi to Norrath = 113
16+
Tambi to Straylight = 130
17+
Tambi to Tristram = 35
18+
Tambi to Arbre = 40
19+
Faerun to Norrath = 63
20+
Faerun to Straylight = 21
21+
Faerun to Tristram = 57
22+
Faerun to Arbre = 83
23+
Norrath to Straylight = 9
24+
Norrath to Tristram = 50
25+
Norrath to Arbre = 60
26+
Straylight to Tristram = 27
27+
Straylight to Arbre = 81
28+
Tristram to Arbre = 90

day9/prog.scala

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import scala.io.Source
2+
import scala.util.Try
3+
4+
val dsts = Source.fromFile("input.txt").getLines
5+
6+
val dstR = """([A-Za-z\d]+) to ([A-Za-z\d]+) = (\d+)""".r
7+
case class Route(p1: String, p2: String, distance: Int)
8+
9+
def updateRoutes(m: Map[String, Map[String, Int]], from: String, to: String, dst: Int) = m.updated(from, m.get(from).getOrElse(Map[String, Int]()).updated(to, dst))
10+
11+
val routes = dsts.map {
12+
case dstR(p1, p2, dst) => Route(p1, p2, dst.toInt)
13+
}.foldLeft(Map[String, Map[String, Int]]()){
14+
case (acc, Route(p1, p2, dst)) => updateRoutes(updateRoutes(acc, p1, p2, dst), p2, p1, dst)
15+
}
16+
17+
def findRoute(place: String, visited: List[String], dst: Int, comparator: (Int, Int) => Boolean): (List[String], Int) = {
18+
routes(place).filter { case (k, _) => !visited.contains(k) && k != place } match {
19+
case s if s.isEmpty => (place :: visited, dst)
20+
case s => s.map { case (p, ds) => findRoute(p, place :: visited, dst + ds, comparator)}.reduceLeft((a, b) => if (comparator(a._2, b._2)) a else b)
21+
}
22+
}
23+
24+
def findLongestOrShortestRoute(shortest: Boolean = true) = {
25+
val shortestComparator = (a: Int, b:Int) => a < b
26+
val comparator = if (shortest) shortestComparator else (a: Int, b: Int) => !shortestComparator(a, b)
27+
28+
routes.keys.map(p => findRoute(p, List(), 0, comparator)).reduceLeft((a, b) => if (comparator(a._2, b._2)) a else b)
29+
}
30+
31+
def findShortestRoute() = findLongestOrShortestRoute(true)
32+
def findLongestRoute() = findLongestOrShortestRoute(false)
33+
34+
val shortest = findShortestRoute()
35+
val longest = findLongestRoute()
36+
37+
println(s"The shortest route is ${shortest._2}: ${shortest._1.mkString(", ")}")
38+
println(s"The longest route is ${longest._2}: ${longest._1.mkString(", ")}")

0 commit comments

Comments
 (0)