This commit is contained in:
Paul-Henri Froidmont 2025-12-02 12:16:13 +01:00
parent 089c7f34db
commit ee395cd25a
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
2 changed files with 47 additions and 0 deletions

46
src/day02.scala Normal file
View file

@ -0,0 +1,46 @@
package aoc
package day02
import scala.annotation.tailrec
val dayNumber = "02"
@main def part1: Unit =
println(part1(loadInput(dayNumber)))
@main def part2: Unit =
println(part2(loadInput(dayNumber)))
def part1(input: String): String =
def isValid(id: String): Boolean =
if id.length % 2 == 0 then
val (left, right) = id.splitAt(id.length / 2)
left != right
else true
input
.split(",")
.map { case s"$from-$to" => from.toLong to to.toLong }
.map(_.filterNot(id => isValid(id.toString)).sum)
.sum
.toString
def part2(input: String): String =
def isValid(id: Long): Boolean =
@tailrec
def rec(id: String, groupsCount: Int): Boolean =
if groupsCount < 1 then true
else if id.length % groupsCount == 0 then
id.grouped(groupsCount).distinct.length > 1 && rec(id, groupsCount - 1)
else rec(id, groupsCount - 1)
rec(id.toString, id.toString.length - 1)
input
.split(",")
.map { case s"$from-$to" => from.toLong to to.toLong }
.map(_.filterNot(isValid).sum)
.sum
.toString