Skip to content

Commit 2d60580

Browse files
committed
refactor: move day01 into separate file
1 parent 20fab68 commit 2d60580

3 files changed

Lines changed: 88 additions & 95 deletions

File tree

2023/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// The simplest possible sbt build file is just one line:
22

3-
scalaVersion := "2.13.12"
3+
ThisBuild / scalaVersion := "2.13.12"
44
// That is, to create a valid sbt build, all you've got to do is define the
55
// version of Scala you'd like your project to use.
66

2023/src/main/scala/Main.scala

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,11 @@
1+
package com.markkovari.adventofcode
2+
13
import scala.io.Source
24
import scala.collection.IndexedSeqView
35
import scala.collection.mutable.HashMap
46

57
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)
10011
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.markkovari.adventofcode
2+
3+
import scala.collection.immutable.HashMap
4+
import scala.io.Source
5+
6+
class Day01 {
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+
private def stringStartsStringifiedDigit(text: String): Option[Int] = {
21+
numbersAsStringsAndValues
22+
.find { case (key, _) => text.startsWith(key) }
23+
.map { case (_, value) => value }
24+
}
25+
26+
private val exampleFilename = "example_1"
27+
private val exampleFilename2 = "example_2"
28+
private val valuesFilename = "values"
29+
30+
private val lines =
31+
Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines
32+
private 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+
private def getFirstAndLastMultipliedTen(list: List[Int]): Int = {
46+
list match {
47+
case a :: Nil => a * 10 + a
48+
case a :: b :: Nil => a * 10 + b
49+
case a :: b :: tail => a * 10 + tail.last
50+
case _ => 0
51+
}
52+
}
53+
54+
def getMixedUpDigits(of: String): List[Int] = of match {
55+
case "" => List()
56+
case other => {
57+
stringStartsStringifiedDigit(other) match {
58+
case Some(value) => value :: getMixedUpDigits(other.splitAt(1)._2)
59+
case None => {
60+
val (head, tail) = other.splitAt(1)
61+
head.toIntOption match {
62+
case None => getMixedUpDigits(tail)
63+
case Some(_) => head.toInt :: getMixedUpDigits(tail)
64+
}
65+
}
66+
}
67+
}
68+
69+
}
70+
71+
def getDigits(of: String): List[Int] = of match {
72+
// TODO: this might be redundant since empty string cannot be converted to int
73+
case "" => List()
74+
case a => {
75+
val (head, tail) = a.splitAt(1)
76+
head.toIntOption match {
77+
case None => getDigits(tail)
78+
case Some(_) => head.toInt :: getDigits(tail)
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)