Skip to content

Instantly share code, notes, and snippets.

@paulmr
Created December 6, 2017 12:10
Show Gist options
  • Save paulmr/8ffaed291fe563543ff04ed51fd3b299 to your computer and use it in GitHub Desktop.
Save paulmr/8ffaed291fe563543ff04ed51fd3b299 to your computer and use it in GitHub Desktop.
Advent 2017-02
// -*- 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