|
| 1 | +package com.markkovari.adventofcode |
| 2 | + |
1 | 3 | import scala.io.Source |
2 | 4 | import scala.collection.IndexedSeqView |
3 | 5 | import scala.collection.mutable.HashMap |
4 | 6 |
|
5 | 7 | object Main extends App { |
6 | | - |
7 | | - private val numbersAsStringsAndValues = |
8 | | - HashMap[String, Int]( |
9 | | - "one" -> 1, |
10 | | - "two" -> 2, |
11 | | - "three" -> 3, |
12 | | - "four" -> 4, |
13 | | - "five" -> 5, |
14 | | - "six" -> 6, |
15 | | - "seven" -> 7, |
16 | | - "eight" -> 8, |
17 | | - "nine" -> 9 |
18 | | - ) |
19 | | - |
20 | | - def stringStartsStringifiedDigit(text: String): Option[Int] = { |
21 | | - numbersAsStringsAndValues |
22 | | - .find { case (key, _) => text.startsWith(key) } |
23 | | - .map { case (_, value) => value } |
24 | | - } |
25 | | - |
26 | | - val exampleFilename = "example_1" |
27 | | - val exampleFilename2 = "example_2" |
28 | | - val valuesFilename = "values" |
29 | | - |
30 | | - val lines = |
31 | | - Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines |
32 | | - val linesForSecond = |
33 | | - Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines |
34 | | - |
35 | | - val firstResult = |
36 | | - lines |
37 | | - .map(line => getFirstAndLastMultipliedTen(getDigits(line))) |
38 | | - .sum |
39 | | - |
40 | | - val secondResult = |
41 | | - linesForSecond |
42 | | - .map(line => getFirstAndLastMultipliedTen(getMixedUpDigits(line))) |
43 | | - .sum |
44 | | - |
45 | | - println(s"firstResult: ${firstResult}") |
46 | | - println(s"secondResultWithMap: ${secondResult}") |
47 | | - |
48 | | - def getFirstAndLastMultipliedTen(list: List[Int]): Int = { |
49 | | - list match { |
50 | | - case a :: Nil => a * 10 + a |
51 | | - case a :: b :: Nil => a * 10 + b |
52 | | - case a :: b :: tail => a * 10 + tail.last |
53 | | - case _ => 0 |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - def getMixedUpDigits(of: String): List[Int] = of match { |
58 | | - case "" => List() |
59 | | - case other => { |
60 | | - stringStartsStringifiedDigit(other) match { |
61 | | - case Some(value) => value :: getMixedUpDigits(other.splitAt(1)._2) |
62 | | - case None => { |
63 | | - val (head, tail) = other.splitAt(1) |
64 | | - head.toIntOption match { |
65 | | - case None => getMixedUpDigits(tail) |
66 | | - case Some(_) => head.toInt :: getMixedUpDigits(tail) |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - } |
73 | | - |
74 | | - // stringStartsStringifiedDigit( |
75 | | - // of |
76 | | - // ) match { |
77 | | - |
78 | | - // case Some(value) => value :: getMixedUpDigits(of.splitAt(1)._2) |
79 | | - // case None => { |
80 | | - // val (head, tail) = of.splitAt(1) |
81 | | - // head.toIntOption match { |
82 | | - // case None => getMixedUpDigits(tail) |
83 | | - // case Some(_) => head.toInt :: getMixedUpDigits(tail) |
84 | | - // } |
85 | | - // } |
86 | | - |
87 | | - // } |
88 | | - |
89 | | - def getDigits(of: String): List[Int] = of match { |
90 | | - // TODO: this might be redundant since empty string cannot be converted to int |
91 | | - case "" => List() |
92 | | - case a => { |
93 | | - val (head, tail) = a.splitAt(1) |
94 | | - head.toIntOption match { |
95 | | - case None => getDigits(tail) |
96 | | - case Some(_) => head.toInt :: getDigits(tail) |
97 | | - } |
98 | | - } |
99 | | - } |
| 8 | + val day = new Day01 |
| 9 | + println(day.firstResult) |
| 10 | + println(day.secondResult) |
100 | 11 | } |
0 commit comments