Skip to content

Commit f30b7c3

Browse files
committed
Solve 2025 day 6 part 2
1 parent e55596c commit f30b7c3

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

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

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,52 @@ object Day6 {
2525
case "*" => Multiply
2626
}
2727

28-
def parseProblems(input: String): Seq[Problem] = {
29-
input
30-
.linesIterator
31-
.map(_.trim.split(" +").toSeq)
32-
.toSeq
33-
.transpose
34-
.map(s => Problem(s.init.map(_.toInt), parseProblemKind(s.last)))
28+
trait Part {
29+
def parseProblems(input: String): Seq[Problem]
30+
}
31+
32+
object Part1 extends Part {
33+
override def parseProblems(input: String): Seq[Problem] = {
34+
input
35+
.linesIterator
36+
.map(_.trim.split(" +").toSeq)
37+
.toSeq
38+
.transpose
39+
.map(s => Problem(s.init.map(_.toInt), parseProblemKind(s.last)))
40+
}
41+
}
42+
43+
object Part2 extends Part {
44+
override def parseProblems(input: String): Seq[Problem] = {
45+
val lines = input.linesIterator.toSeq
46+
val maxLength = lines.map(_.length).max
47+
val paddedLines = lines.map(_.padTo(maxLength, ' ')) // pad because test code has trimmed trailing whitespace
48+
val cols = paddedLines.transpose
49+
50+
// TODO: split on Seq?
51+
def helper(cols: Seq[Seq[Char]]): List[Problem] = {
52+
val (problemCols, newCols) = cols.span(!_.forall(_ == ' '))
53+
val problemKind = parseProblemKind(problemCols.head.last.toString)
54+
val nums = problemCols.map(_.init.mkString("").trim.toInt)
55+
val problem = Problem(nums, problemKind)
56+
val rest = {
57+
if (newCols.isEmpty)
58+
Nil
59+
else
60+
helper(newCols.tail)
61+
}
62+
problem :: rest
63+
}
64+
65+
helper(cols)
66+
}
3567
}
3668

3769
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day6.txt")).mkString.trim
3870

3971
def main(args: Array[String]): Unit = {
40-
println(sumAnswers(parseProblems(input)))
72+
println(sumAnswers(Part1.parseProblems(input)))
73+
println(sumAnswers(Part2.parseProblems(input)))
4174

4275
// part 1: 1615951811 - too low (Int overflowed in Problem#answer)
4376
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ class Day6Test extends AnyFunSuite {
1212
|* + * +""".stripMargin
1313

1414
test("Part 1 examples") {
15-
assert(sumAnswers(parseProblems(exampleInput)) == 4277556)
15+
assert(sumAnswers(Part1.parseProblems(exampleInput)) == 4277556)
1616
}
1717

1818
test("Part 1 input answer") {
19-
assert(sumAnswers(parseProblems(input)) == 5361735137219L)
19+
assert(sumAnswers(Part1.parseProblems(input)) == 5361735137219L)
20+
}
21+
22+
test("Part 2 examples") {
23+
assert(sumAnswers(Part2.parseProblems(exampleInput)) == 3263827)
24+
}
25+
26+
test("Part 2 input answer") {
27+
assert(sumAnswers(Part2.parseProblems(input)) == 11744693538946L)
2028
}
2129
}

0 commit comments

Comments
 (0)