From a467472e836b142f5ce9277a0b01317968a3192e Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 4 Dec 2018 18:01:49 +0100 Subject: [PATCH] Adding day 1 --- day1/part1.scala | 12 ++++++++++++ day1/part2.scala | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 day1/part1.scala create mode 100644 day1/part2.scala diff --git a/day1/part1.scala b/day1/part1.scala new file mode 100644 index 0000000..ac5496f --- /dev/null +++ b/day1/part1.scala @@ -0,0 +1,12 @@ +/** + * Usage: scala part1.scala < input + */ + +import scala.io.StdIn +import scala.collection.Iterator + +println(Iterator + .continually(StdIn.readLine) + .takeWhile(_ != null) + .map(Integer.parseInt) + .sum) diff --git a/day1/part2.scala b/day1/part2.scala new file mode 100644 index 0000000..bab57ee --- /dev/null +++ b/day1/part2.scala @@ -0,0 +1,23 @@ +/** + * Usage: scala part2.scala < input + */ + +import scala.io.StdIn +import scala.collection.Iterator + +val items = Stream + .continually(StdIn.readLine) + .takeWhile(_ != null) + .map(Integer.parseInt) + .toList + +println( +Stream + .continually(items.toStream) + .flatten + .scanLeft(0)(_ + _) + .scanLeft(Map.empty[Int, Int]){case (acc, item) => acc + (item -> (acc.getOrElse(item, 0) + 1)) } + .filter(_.count(_._2 == 2) > 0) + .map(_.maxBy(_._2)) + .head +)