Skip to content

Commit 81d3912

Browse files
committed
chore: reformat scala since did not touch this for months
1 parent ef32c8b commit 81d3912

12 files changed

Lines changed: 111 additions & 107 deletions

File tree

2023/src/main/scala/day01.scala renamed to 2023/src/main/scala/com/markkovari/adventofcode/day01.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.markkovari.adventofcode.day01
22

33
import scala.collection.immutable.HashMap
4-
import scala.io.Source
54

65
val numbersAsStringsAndValues =
76
HashMap(
@@ -16,7 +15,7 @@ val numbersAsStringsAndValues =
1615
"nine" -> 9
1716
)
1817

19-
def stringStartsStringifiedDigit(text: String): Option[Int] =
18+
def stringStartsStringifyDigit(text: String): Option[Int] =
2019
numbersAsStringsAndValues
2120
.find { case (key, _) => text.startsWith(key) }
2221
.map { case (_, value) => value }
@@ -32,7 +31,7 @@ def getFirstAndLastMultipliedTen(list: List[Int]): Int =
3231
def getMixedUpDigits(of: String): List[Int] = of match {
3332
case "" => List()
3433
case other =>
35-
stringStartsStringifiedDigit(other) match {
34+
stringStartsStringifyDigit(other) match {
3635
case Some(value) => value :: getMixedUpDigits(other.splitAt(1)._2)
3736
case None =>
3837
val (head, tail) = other.splitAt(1)

2023/src/main/scala/day02.scala renamed to 2023/src/main/scala/com/markkovari/adventofcode/day02.scala

File renamed without changes.

2023/src/main/scala/day03.scala renamed to 2023/src/main/scala/com/markkovari/adventofcode/day03.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import scala.util.matching.Regex.Match
44

55
case class Detail(content: String, x: Int, y: Int)
66

7-
case class Coord(x: Int, y: Int):
8-
def within(start: Coord, end: Coord) =
7+
case class Coordinate(x: Int, y: Int):
8+
def within(start: Coordinate, end: Coordinate) =
99
if y < start.y || y > end.y then false
1010
else if x < start.x || x > end.x then false
1111
else true
1212

13-
case class PartNumber(value: Int, start: Coord, end: Coord)
13+
case class PartNumber(value: Int, start: Coordinate, end: Coordinate)
1414

15-
case class Symbol(sym: String, pos: Coord):
15+
case class Symbol(sym: String, pos: Coordinate):
1616
def neighborOf(number: PartNumber) = pos.within(
17-
Coord(number.start.x - 1, number.start.y - 1),
18-
Coord(number.end.x + 1, number.end.y + 1)
17+
Coordinate(number.start.x - 1, number.start.y - 1),
18+
Coordinate(number.end.x + 1, number.end.y + 1)
1919
)
2020

2121
object IsInt:
@@ -33,8 +33,8 @@ def findPartsAndSymbols(
3333
.findAllMatchIn(line)
3434
.map:
3535
case m @ IsInt(nb) =>
36-
PartNumber(nb, Coord(m.start, i), Coord(m.end - 1, i))
37-
case s => Symbol(s.matched, Coord(s.start, i))
36+
PartNumber(nb, Coordinate(m.start, i), Coordinate(m.end - 1, i))
37+
case s => Symbol(s.matched, Coordinate(s.start, i))
3838

3939
def part1(input: String) =
4040
val all = findPartsAndSymbols(input)

2023/src/main/scala/day04.scala renamed to 2023/src/main/scala/com/markkovari/adventofcode/day04.scala

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.markkovari.adventofcode.day04
22

3-
import scala.util.matching.Regex.Match
4-
53
case class Card(id: Int, numbers: List[Int], winningNumbers: List[Int]) {
64
private def matches(): Set[Int] = {
75
numbers.toSet.intersect(winningNumbers.toSet)
@@ -17,8 +15,8 @@ def parseCard(input: String): Card =
1715
case s"Card ${id}: ${numbers} | ${winningNumbers}" =>
1816
Card(
1917
id.trim().toInt,
20-
numbers.split(" ").filter(!_.isEmpty).map(_.toInt).toList,
21-
winningNumbers.split(" ").filter(!_.isEmpty).map(_.toInt).toList
18+
numbers.split(" ").filter(_.nonEmpty).map(_.toInt).toList,
19+
winningNumbers.split(" ").filter(_.nonEmpty).map(_.toInt).toList
2220
)
2321
}
2422

@@ -44,9 +42,9 @@ def countWinning(card: String): Int =
4442
.substring(card.indexOf(":") + 1)
4543
.split(" ")
4644
.filterNot(_.isEmpty())
47-
val (winningNumberStrs, givenNumberStrs) = numbers.span(_ != "|")
48-
val winningNumbers = winningNumberStrs.map(_.toInt).toSet
49-
val givenNumbers = givenNumberStrs.drop(1).map(_.toInt).toSet
45+
val (winningNumberStrings, givenNumberStrings) = numbers.span(_ != "|")
46+
val winningNumbers = winningNumberStrings.map(_.toInt).toSet
47+
val givenNumbers = givenNumberStrings.drop(1).map(_.toInt).toSet
5048
winningNumbers.intersect(givenNumbers).size
5149

5250
def winningCounts(input: String): Iterator[Int] =
@@ -56,7 +54,7 @@ def part1_(input: String): String =
5654
winningCounts(input)
5755
.map(winning => if winning > 0 then Math.pow(2, winning - 1).toInt else 0)
5856
.sum
59-
.toString()
57+
.toString
6058

6159
def part2_(input: String): String =
6260
winningCounts(input)
@@ -70,4 +68,4 @@ def part2_(input: String): String =
7068
(numCards + thisMult, restMult)
7169
}
7270
._1
73-
.toString()
71+
.toString

2023/src/main/scala/day05.scala renamed to 2023/src/main/scala/com/markkovari/adventofcode/day05.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.markkovari.adventofcode.day05
22

3-
import scala.util.matching.Regex.Match
4-
53
final case class Resource(start: Long, end: Long, kind: ResourceKind)
64

75
enum ResourceKind:
@@ -98,12 +96,10 @@ object Seeds:
9896
.split(" ")
9997
.flatMap(_.toLongOption)
10098

101-
// parse seeds without range
10299
def parseWithoutRange(line: String): Seq[Resource] =
103100
parseSeedsRaw(line).map: start =>
104101
Resource(start, start, ResourceKind.Seed)
105102

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

2023/src/test/scala/day02.scala renamed to 2023/src/test/scala/com/markkovari/adventofcode/Day02Tests.scala

File renamed without changes.

2023/src/test/scala/day03.scala renamed to 2023/src/test/scala/com/markkovari/adventofcode/Day03Tests.scala

File renamed without changes.

2023/src/test/scala/day04.scala renamed to 2023/src/test/scala/com/markkovari/adventofcode/Day04Tests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.markkovari.adventofcode
22

3+
import com.markkovari.adventofcode.day04.*
34
import org.scalatest.funsuite.AnyFunSuite
5+
46
import scala.io.Source
5-
import scala.collection.immutable.HashMap
6-
import day04._
77

88
class Day04Tests extends AnyFunSuite {
99

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.markkovari.adventofcode
2+
3+
import com.markkovari.adventofcode.day05.*
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
import scala.io.Source
7+
import scala.util.Using
8+
9+
class Day05Tests extends AnyFunSuite {
10+
11+
test("Part1 example") {
12+
Using(io.Source.fromFile("./src/test/resources/5/example_1")) { source =>
13+
val input = source.mkString
14+
val result = part1(input)
15+
assert(result == 35)
16+
}
17+
}
18+
19+
test("Part1 is calculated") {
20+
Using(io.Source.fromFile("./src/test/resources/5/values")) { source =>
21+
val input = source.mkString
22+
val result = part1(input)
23+
assert(result == 1182)
24+
}
25+
}
26+
27+
test("Part2 example") {
28+
Using(io.Source.fromFile("./src/test/resources/5/example_1")) { source =>
29+
val input = source.mkString
30+
val result = part2(input)
31+
assert(result == 8)
32+
}
33+
}
34+
35+
test("Part2 is calculated") {
36+
Using(io.Source.fromFile("./src/test/resources/5/values")) { source =>
37+
val input = source.mkString
38+
val result = part2(input)
39+
assert(result == 1181555926)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)