Skip to content

Commit 45364b4

Browse files
committed
Solve 2025 day 2 part 1
1 parent c56403a commit 45364b4

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9191896883-9191940271,457499-518693,4952-6512,960-1219,882220-1039699,2694-3465,3818-4790,166124487-166225167,759713819-759869448,4821434-4881387,7271-9983,1182154-1266413,810784-881078,802-958,1288-1491,45169-59445,25035-29864,379542-433637,287-398,75872077-75913335,653953-689335,168872-217692,91-113,475-590,592-770,310876-346156,2214325-2229214,85977-112721,51466993-51620441,8838997-8982991,534003-610353,32397-42770,17-27,68666227-68701396,1826294188-1826476065,1649-2195,141065204-141208529,7437352-7611438,10216-13989,33-44,1-16,49-74,60646-73921,701379-808878
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package eu.sim642.adventofcode2025
2+
3+
import eu.sim642.adventofcode2016.Day20.Interval
4+
5+
object Day2 {
6+
7+
type Range = Interval
8+
9+
val invalidIds: LazyList[Long] = LazyList.from(1).map(i =>
10+
val len = i.toString.length // TODO: already have a (better) way to get number of digits?
11+
i * (Math.powExact(10L, len) + 1)
12+
)
13+
14+
def sumInvalidIds(ranges: Seq[Range]): Long = { // TODO: Long?
15+
val max = ranges.map(_.max).max
16+
invalidIds
17+
.takeWhile(_ <= max)
18+
.filter(id =>
19+
ranges.exists(_.contains(id))
20+
)
21+
.sum
22+
}
23+
24+
def parseRange(s: String): Range = s match {
25+
case s"$i-$j" => Interval(i.toLong, j.toLong)
26+
}
27+
28+
def parseRanges(input: String): Seq[Interval] = input.split(",").toSeq.map(parseRange)
29+
30+
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day2.txt")).mkString.trim
31+
32+
def main(args: Array[String]): Unit = {
33+
println(sumInvalidIds(parseRanges(input)))
34+
}
35+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package eu.sim642.adventofcode2025
2+
3+
import Day2._
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
class Day2Test extends AnyFunSuite {
7+
8+
val exampleInput =
9+
"""11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124"""
10+
11+
test("Part 1 examples") {
12+
assert(sumInvalidIds(parseRanges(exampleInput)) == 1227775554)
13+
}
14+
15+
test("Part 1 input answer") {
16+
assert(sumInvalidIds(parseRanges(input)) == 5398419778L)
17+
}
18+
}

0 commit comments

Comments
 (0)