Skip to content

Commit 816e5ba

Browse files
committed
chore: switych to TDD-like result matching
1 parent 649d225 commit 816e5ba

3 files changed

Lines changed: 58 additions & 26 deletions

File tree

2023/src/main/scala/day01.scala

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package com.markkovari.adventofcode.day01
33
import scala.collection.immutable.HashMap
44
import scala.io.Source
55

6-
@main def part1: Unit = println(s"The solution is ${firstResult}")
7-
@main def part2: Unit = println(s"The solution is ${secondResult}")
8-
9-
private val numbersAsStringsAndValues =
6+
val numbersAsStringsAndValues =
107
HashMap(
118
"one" -> 1,
129
"two" -> 2,
@@ -19,32 +16,12 @@ private val numbersAsStringsAndValues =
1916
"nine" -> 9
2017
)
2118

22-
private def stringStartsStringifiedDigit(text: String): Option[Int] =
19+
def stringStartsStringifiedDigit(text: String): Option[Int] =
2320
numbersAsStringsAndValues
2421
.find { case (key, _) => text.startsWith(key) }
2522
.map { case (_, value) => value }
2623

27-
private val exampleFilename = "example_1"
28-
private val exampleFilename2 = "example_2"
29-
private val valuesFilename = "values"
30-
31-
private val lines =
32-
Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines
33-
34-
private val linesForSecond =
35-
Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines
36-
37-
val firstResult =
38-
lines
39-
.map(line => getFirstAndLastMultipliedTen(getDigits(line)))
40-
.sum
41-
42-
val secondResult =
43-
linesForSecond
44-
.map(line => getFirstAndLastMultipliedTen(getMixedUpDigits(line)))
45-
.sum
46-
47-
private def getFirstAndLastMultipliedTen(list: List[Int]): Int =
24+
def getFirstAndLastMultipliedTen(list: List[Int]): Int =
4825
list match {
4926
case a :: Nil => a * 10 + a
5027
case a :: b :: Nil => a * 10 + b

2023/src/test/scala/day01.scala

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.markkovari.adventofcode
2+
3+
import org.scalatest.funsuite.AnyFunSuite
4+
import scala.io.Source
5+
import scala.collection.immutable.HashMap
6+
import day02._
7+
import com.markkovari.adventofcode.day01.getFirstAndLastMultipliedTen
8+
import com.markkovari.adventofcode.day01.getDigits
9+
import com.markkovari.adventofcode.day01.getMixedUpDigits
10+
11+
class Day01Tests extends AnyFunSuite {
12+
13+
private val exampleFilename = "example_1"
14+
private val exampleFilename2 = "example_2"
15+
private val valuesFilename = "values"
16+
17+
test("example result is the same as in the description") {
18+
val lines =
19+
Source.fromFile(s"./src/main/resources/1/${exampleFilename}").getLines
20+
21+
val firstResult =
22+
lines
23+
.map(line => getFirstAndLastMultipliedTen(getDigits(line)))
24+
.sum
25+
assert(firstResult == 142)
26+
}
27+
28+
test("result first part") {
29+
val lines =
30+
Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines
31+
32+
val firstResult =
33+
lines
34+
.map(line => getFirstAndLastMultipliedTen(getDigits(line)))
35+
.sum
36+
assert(firstResult == 54573)
37+
38+
val linesForSecond =
39+
Source.fromFile(s"./src/main/resources/1/${valuesFilename}").getLines
40+
val secondResult =
41+
linesForSecond
42+
.map(line => getFirstAndLastMultipliedTen(getMixedUpDigits(line)))
43+
.sum
44+
45+
assert(secondResult == 54591)
46+
}
47+
48+
}

2023/src/test/scala/day02.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ class Day02Tests extends AnyFunSuite {
2121
assert(result == 2617)
2222
}
2323

24+
test("result is the same as in the description part 2") {
25+
26+
val text = Source.fromFile("./src/main/resources/2/values").mkString
27+
val result = part2(text)
28+
assert(result == 59795)
29+
}
30+
2431
}

0 commit comments

Comments
 (0)