@@ -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 }
0 commit comments