From 8b542f4b14bc7c5bbe2b6df8f914da0f5d88f48c Mon Sep 17 00:00:00 2001 From: scarf Date: Mon, 26 Aug 2024 01:50:02 +0900 Subject: [PATCH] feat: day 10 --- 2015/Day10.scala | 23 +++++++++++++++++++++++ 2015/Day10.test.scala | 8 ++++++++ 2 files changed, 31 insertions(+) create mode 100644 2015/Day10.scala create mode 100644 2015/Day10.test.scala diff --git a/2015/Day10.scala b/2015/Day10.scala new file mode 100644 index 0000000..4b9d89b --- /dev/null +++ b/2015/Day10.scala @@ -0,0 +1,23 @@ +package day10 + +import scala.io.Source.fromFile + +val groupNums = raw"(.)\1*".r + +def nextNums(s: String) = groupNums + .findAllIn(s) + .map { xs => s"${xs.length}${xs.head}" } + .mkString + +def solution(input: String)(n: Int) = + (1 to n).foldLeft(input) { (acc, _) => nextNums(acc) }.length + +@main def main() = + val input = fromFile(".cache/10.txt").mkString.trim + val solver = solution(input) + + val part1 = solver(40) + println(part1) + + val part2 = solver(50) + println(part2) diff --git a/2015/Day10.test.scala b/2015/Day10.test.scala new file mode 100644 index 0000000..1e2126b --- /dev/null +++ b/2015/Day10.test.scala @@ -0,0 +1,8 @@ +import munit.FunSuite +import day10.* + +class Day10Tests extends FunSuite: + test("Part 1"): + Seq("1", "11", "21", "1211", "111221", "312211") + .sliding(2) + .map { case Seq(a, b) => assertEquals(nextNums(a), b) }