-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day09.fs
20 lines (16 loc) · 961 Bytes
/
Day09.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module Year2017Day09
open AdventOfCode.FSharp.Common
type GarbageState = NotGarbage | Garbage | Cancelled
type State = {level: int; state: GarbageState; score: int; garbage: int }
let step current nextChar =
match (current.state, nextChar) with
| (Garbage, '!') -> {current with state = Cancelled}
| (Garbage, '>') -> {current with state = NotGarbage}
| (Garbage, _) -> {current with garbage = current.garbage + 1}
| (Cancelled, _) -> {current with state = Garbage}
| (NotGarbage, '{') -> {current with level = current.level + 1}
| (NotGarbage, '}') -> {current with level = current.level - 1; score = current.score + current.level}
| (NotGarbage, '<') -> {current with state = Garbage}
| _ -> current;
let solve = Seq.fold step {level=0; state=NotGarbage; score=0; garbage=0}
let solver = {parse = parseFirstLine asString; part1 = solve >> (fun state -> state.score); part2 = solve >> (fun state -> state.garbage)}