Skip to content

Commit 89824ad

Browse files
committed
Solve 2025 day 1 part 2
1 parent 0b85eca commit 89824ad

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/main/scala/eu/sim642/adventofcode2025/Day1.scala

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,28 @@ import eu.sim642.adventofcodelib.IntegralImplicits._
44

55
object Day1 {
66

7-
def actualPassword(rotations: Seq[Int]): Int = {
8-
rotations
9-
.scanLeft[Int](50)((a, b) => (a + b) %+ 100) // TODO: why can't use implicit arguments?
10-
.count(_ == 0)
7+
trait Part {
8+
def password(rotations: Seq[Int]): Int
9+
}
10+
11+
object Part1 extends Part {
12+
override def password(rotations: Seq[Int]): Int = {
13+
rotations
14+
.scanLeft[Int](50)((a, b) => (a + b) %+ 100) // TODO: why can't use implicit arguments?
15+
.count(_ == 0)
16+
}
17+
}
18+
19+
object Part2 extends Part {
20+
override def password(rotations: Seq[Int]): Int = {
21+
rotations
22+
.flatMap({ // expand all rotations to single to make each tick observable, this is silly but works
23+
case i if i >= 0 => Seq.fill(i)(1)
24+
case i if i < 0 => Seq.fill(-i)(-1)
25+
})
26+
.scanLeft[Int](50)((a, b) => (a + b) %+ 100) // TODO: why can't use implicit arguments?
27+
.count(_ == 0)
28+
}
1129
}
1230

1331
def parseRotation(s: String): Int = s match {
@@ -20,6 +38,7 @@ object Day1 {
2038
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day1.txt")).mkString.trim
2139

2240
def main(args: Array[String]): Unit = {
23-
println(actualPassword(parseRotations(input)))
41+
println(Part1.password(parseRotations(input)))
42+
println(Part2.password(parseRotations(input)))
2443
}
2544
}

src/test/scala/eu/sim642/adventofcode2025/Day1Test.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ class Day1Test extends AnyFunSuite {
1717
|R14
1818
|L82""".stripMargin
1919

20-
test("Part 1 example") {
21-
assert(actualPassword(parseRotations(exampleInput)) == 3)
20+
test("Part 1 examples") {
21+
assert(Part1.password(parseRotations(exampleInput)) == 3)
2222
}
2323

2424
test("Part 1 input answer") {
25-
assert(actualPassword(parseRotations(input)) == 995)
25+
assert(Part1.password(parseRotations(input)) == 995)
26+
}
27+
28+
test("Part 2 examples") {
29+
assert(Part2.password(parseRotations(exampleInput)) == 6)
30+
assert(Part2.password(parseRotations("R1000")) == 10)
31+
}
32+
33+
test("Part 2 input answer") {
34+
assert(Part2.password(parseRotations(input)) == 5847)
2635
}
2736
}

0 commit comments

Comments
 (0)