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(""))