From d4d1f956193bbaf12cd3242e57747114e75a7297 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 5 Dec 2018 10:03:12 +0100 Subject: [PATCH] Adding day 2 --- day2/part1.scala | 18 ++++++++++++++++++ day2/part2.scala | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 day2/part1.scala create mode 100644 day2/part2.scala diff --git a/day2/part1.scala b/day2/part1.scala new file mode 100644 index 0000000..6f0c9cc --- /dev/null +++ b/day2/part1.scala @@ -0,0 +1,18 @@ +import scala.io.StdIn._ + +val res = Iterator + .continually(readLine) + .takeWhile(_ != null) + .toList + .map { + entry => val counts = entry + .groupBy(_.charValue) + .mapValues(_.length) + .values + (counts.find(_ == 2), counts.find(_ == 3)) + } + .foldLeft((0, 0)) { + (acc, item) => (acc._1 + item._1.map(_ => 1).getOrElse(0), + acc._2 + item._2.map(_ => 1).getOrElse(0)) } + +println(res._1 * res._2) diff --git a/day2/part2.scala b/day2/part2.scala new file mode 100644 index 0000000..bec60d6 --- /dev/null +++ b/day2/part2.scala @@ -0,0 +1,16 @@ +import scala.io.StdIn._ + + +val items = Iterator + .continually(readLine) + .takeWhile(_ != null) + .toList + +val res = items + .combinations(2) + .map(x => x(0) zip x(1)) + .dropWhile(_.count(t => t._1 != t._2) != 1) + .map(_.filter(t => t._1 == t._2).map(_._1)) + .next + +println(res.mkString(""))