8000 Solve MITS 2024 day 1 · sim642/adventofcode@dd114cd · GitHub
[go: up one dir, main page]

Skip to content

Commit dd114cd

Browse files
committed
Solve MITS 2024 day 1
1 parent 8113eb1 commit dd114cd

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Kelluke: 1.67
2+
ForMe: 1.93
3+
Dynamit: 0.97
4+
mfKaDynamituForMeForMedeuDDdFyyeiyoeFKyDmfKDynamitForMeKKellukepForMeuupFFamDynamitDynamitumuayForMetKellukeDKellukeoKellukemKellukeuDfyeForMeeFaKellukeDprpKellukeaMyyKitpytFdeMKyKmDynamitFatKellukeuForMemDynamitDynamitriDynamitdaMMDynamitotDynamitKioydrdeirfpDynamitrFuyuKmrdKellukeKelluketKoFoFForMedyKaaeriKellukeotMymDynamitKellukefodpDuFDyFtrdDMurpForMeDynamittDynamitDynamitepKaForMeimaDynamitdaopKForMeuDynamitfdopueoDynamitForMeuiDynamityiyDmfForMeyoDKellukeDynamitapDynamitKmForMeDynamitMDynamitymtKellukeForMeufumFfyiKirea
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package eu.sim642.mits2024
2+
3+
import eu.sim642.adventofcodelib.IteratorImplicits.*
4+
5+
object Day1 {
6+
7+
case class Input(kelluke: BigDecimal, forMe: BigDecimal, dynamit: BigDecimal, order: String) {
8+
def orderTotal: BigDecimal = {
9+
val it = """(Kelluke|ForMe|Dynamit)""".r.findAllMatchIn(order)
10+
val freqs = it.groupCount(_.matched).withDefaultValue(0)
11+
freqs("Kelluke") * kelluke + freqs("ForMe") * forMe + freqs("Dynamit") * dynamit
12+
}
13+
}
14+
15+
def parseInput(input: String): Input = input match {
16+
case s"Kelluke: $kelluke\nForMe: $forMe\nDynamit: $dynamit\n$order" => Input(BigDecimal(kelluke), BigDecimal(forMe), BigDecimal(dynamit), order)
17+
}
18+
19+
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day1.txt")).mkString.trim
20+
21+
def main(args: Array[String]): Unit = {
22+
println(parseInput(input).orderTotal)
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package eu.sim642.mits2024
2+
3+
import Day1._
4+
import org.scalatest.funsuite.AnyFunSuite
5+
6+
class Day1Test extends AnyFunSuite {
7+
8+
val exampleInput =
9+
"""Kelluke: 1.64
10+
|ForMe: 1.89
11+
|Dynamit: 0.92
12+
|odForMedFuDrDynamiti""".stripMargin
13+
14+
test("Example") {
15+
assert(parseInput(exampleInput).orderTotal == 2.81)
16+
}
17+
18+
test("Input answer") {
19+
assert(parseInput(input).orderTotal ==
20+
74.64)
21+
}
22+
}

0 commit comments

Comments
 (0)
0