-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day09.scala
26 lines (21 loc) · 1.02 KB
/
Day09.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
package AdventOfCode2017
object Day09:
case class State(groupDepth: Int, groupScore: Int, garbage: Boolean, garbageScore: Int, ignoreNext: Boolean):
def step(next: Char): State = next match
case _ if ignoreNext => copy(ignoreNext = false)
case '!' => copy(ignoreNext = true)
case '>' => copy(garbage = false)
case _ if garbage => copy(garbageScore = garbageScore + 1)
case '{' => copy(groupDepth = groupDepth + 1, groupScore = groupScore + groupDepth)
case '}' => copy(groupDepth = groupDepth - 1)
case '<' => copy(garbage = true)
case _ => this
def stream(input: String): State =
val initial = State(1, 0, false, 0, false)
input.foldLeft(initial)((state, next) => state.step(next))
def part1(input: String): Int = stream(input).groupScore
def part2(input: String): Int = stream(input).garbageScore
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2017/Day09.txt").mkString.trim
println(part1(data))
println(part2(data))