Skip to content

Commit

Permalink
feat: day 10
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Aug 25, 2024
1 parent cfb8d1c commit 8b542f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 2015/Day10.scala
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions 2015/Day10.test.scala
Original file line number Diff line number Diff line change
@@ -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) }

0 comments on commit 8b542f4

Please sign in to comment.