@@ -6,8 +6,8 @@ import scala.io.Source
66@ main def part1 : Unit = println(s " The solution is ${firstResult}" )
77@ main def part2 : Unit = println(s " The solution is ${secondResult}" )
88
9- val numbersAsStringsAndValues =
10- HashMap [ String , Int ] (
9+ private val numbersAsStringsAndValues =
10+ HashMap (
1111 " one" -> 1 ,
1212 " two" -> 2 ,
1313 " three" -> 3 ,
@@ -19,18 +19,18 @@ val numbersAsStringsAndValues =
1919 " nine" -> 9
2020 )
2121
22- private def stringStartsStringifiedDigit (text : String ): Option [Int ] = {
22+ private def stringStartsStringifiedDigit (text : String ): Option [Int ] =
2323 numbersAsStringsAndValues
2424 .find { case (key, _) => text.startsWith(key) }
2525 .map { case (_, value) => value }
26- }
2726
2827private val exampleFilename = " example_1"
2928private val exampleFilename2 = " example_2"
3029private val valuesFilename = " values"
3130
3231private val lines =
3332 Source .fromFile(s " ./src/main/resources/1/ ${valuesFilename}" ).getLines
33+
3434private val linesForSecond =
3535 Source .fromFile(s " ./src/main/resources/1/ ${valuesFilename}" ).getLines
3636
@@ -44,39 +44,34 @@ val secondResult =
4444 .map(line => getFirstAndLastMultipliedTen(getMixedUpDigits(line)))
4545 .sum
4646
47- private def getFirstAndLastMultipliedTen (list : List [Int ]): Int = {
47+ private def getFirstAndLastMultipliedTen (list : List [Int ]): Int =
4848 list match {
4949 case a :: Nil => a * 10 + a
5050 case a :: b :: Nil => a * 10 + b
5151 case a :: b :: tail => a * 10 + tail.last
5252 case _ => 0
5353 }
54- }
5554
5655def getMixedUpDigits (of : String ): List [Int ] = of match {
5756 case " " => List ()
58- case other => {
57+ case other =>
5958 stringStartsStringifiedDigit(other) match {
6059 case Some (value) => value :: getMixedUpDigits(other.splitAt(1 )._2)
61- case None => {
60+ case None =>
6261 val (head, tail) = other.splitAt(1 )
6362 head.toIntOption match {
64- case None => getMixedUpDigits(tail)
65- case Some (_ ) => head.toInt :: getMixedUpDigits(tail)
63+ case None => getMixedUpDigits(tail)
64+ case Some (value ) => value :: getMixedUpDigits(tail)
6665 }
67- }
6866 }
69- }
70-
7167}
7268
7369def getDigits (of : String ): List [Int ] = of match {
7470 case " " => List ()
75- case a => {
71+ case a =>
7672 val (head, tail) = a.splitAt(1 )
7773 head.toIntOption match {
78- case None => getDigits(tail)
79- case Some (_ ) => head.toInt :: getDigits(tail)
74+ case None => getDigits(tail)
75+ case Some (value ) => value :: getDigits(tail)
8076 }
81- }
8277}
0 commit comments