-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01Test.scala
49 lines (39 loc) · 1.24 KB
/
Day01Test.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package org.lemon.advent.year2022
import scala.io.Source
import org.lemon.advent._
class Day01Test extends UnitTest {
def parse(input: String) = Source.fromString(input).getLines.toSeq
def groupByElf(input: Seq[String]): Seq[Seq[String]] = input match
case Seq() => Seq.empty[Seq[String]]
case seq => Seq(seq.takeWhile(!_.isBlank())) ++
groupByElf(seq.dropWhile(!_.isBlank()).dropWhile(_.isBlank()))
def parseCarry(elves: Seq[Seq[String]]): Seq[Int] = elves.map(_.map(_.toInt).sum)
def getMax(elves: Seq[Int]) = elves.max
def getSexiestElf = parse andThen groupByElf andThen parseCarry andThen getMax
test("day 1 example") {
val exampleInput = """
|1000
|2000
|3000
|
|4000
|
|5000
|6000
|
|7000
|8000
|9000
|
|10000
""".stripMargin.stripLeading
getSexiestElf(exampleInput) shouldBe 24000
}
test("day 1 part 1") {
getSexiestElf(read(file(2022)(1))) shouldBe 69795
}
test("day 1 part 2") {
val carry = (parse andThen groupByElf andThen parseCarry)(read(file(2022)(1)))
carry.sorted.takeRight(3).sum shouldBe 208437
}
}