Created
December 6, 2017 12:10
-
-
Save paulmr/8ffaed291fe563543ff04ed51fd3b299 to your computer and use it in GitHub Desktop.
Advent 2017-02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- mode: scala -*- | |
import scala.io.Source | |
def input(fname: String): List[List[Int]] = input(Source.fromFile(fname)) | |
def input(in: Source): List[List[Int]] = in.getLines.toList.map(_.split("[\t ]+").toList.map(_.toInt)) | |
def calc(data: List[List[Int]]) = | |
data.map(row => { | |
val s = row.sorted | |
s.last - s.head | |
}).sum | |
def findCombs[A](l: List[A]): List[(A, A)] = { | |
for(a <- l; lA <- l if lA != a) yield (a, lA) | |
} | |
def find(l: List[Int]) = findCombs(l).collectFirst { | |
case (a, b) if a % b == 0 => a / b | |
} | |
def calc2(data: List[List[Int]]) = | |
data.map(row => find(row).get).sum | |
@main | |
def go() = { | |
val data = input("input02.txt") | |
println(s"1: ${calc(data)}") | |
println(s"2: ${calc2(data)}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment