Created
December 4, 2018 05:49
-
-
Save muety/5126a97b8b8374f6920c027227101601 to your computer and use it in GitHub Desktop.
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
import scala.io.Source | |
object Advent { | |
def main(args: Array[String]): Unit = { | |
val data = Source.fromFile("data/day1.txt").getLines.map(_.toInt).toList | |
println(f"Part 1: ${data.sum}") | |
/* Can someone help me get this second part a littler more Scala-like (more functional, less procedural)? Please let me know! */ | |
var seen: Set[Int] = Set() | |
var dup: Option[Int] = None | |
var start: Int = 0 | |
while (dup.isEmpty) { | |
val scanned = data.scanLeft(start)(_ + _).drop(1) | |
dup = scanned.find(seen.contains(_)) | |
seen = seen ++ scanned | |
start = scanned.last | |
} | |
println(f"Part 2: ${dup.get}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment