-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day03_fparsec.fs
91 lines (64 loc) · 2.57 KB
/
Day03_fparsec.fs
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
module aoc24.Day03fparsec
open FParsec
type UserState =
{ InsideDo: bool
IgnoreDont: bool }
static member Default = { InsideDo = true; IgnoreDont = false }
static member DefaultIgnoreDont =
{ UserState.Default with
IgnoreDont = true }
let setUsInsideDo e =
updateUserState (fun us -> { us with InsideDo = e })
let usSatisfiesInsideDo =
userStateSatisfies (fun us -> us.InsideDo || us.IgnoreDont)
let do_ = skipString "do()" .>> setUsInsideDo true
let dont_ = skipString "don't()" .>> setUsInsideDo false
let skips = choice [ do_; dont_; skipAnyChar ]
let mul =
let args = pint32 .>> pchar ',' .>>. pint32
let mul = (pstring "mul(") >>. args .>> (pstring ")")
usSatisfiesInsideDo >>. mul
let parser =
let nextMul = skipManyTill skips (followedBy mul) >>. mul
nextMul |> attempt |> many
let parse userState input =
runParserOnString parser userState "" input
|> function
| Success(result, _, _) -> result
| Failure(msg, _, _) -> failwith msg
let mulSums = Seq.sumBy (TupleEx.apply (*))
let part1 input =
input |> parse UserState.DefaultIgnoreDont |> mulSums
let part2 input =
input |> parse UserState.Default |> mulSums
open Swensen.Unquote
open Xunit
module tests =
let example1 =
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
[<Fact>]
let ``Part 1 example`` () = part1 example1 =! 161
[<Theory>]
[<InlineData("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))")>]
[<InlineData("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()\n?mul(8,5))")>]
let ``Part 2 example`` (input) = part2 input =! 48
let testfile = "../../../inputs/day03.txt"
let testfileExists = System.IO.File.Exists(testfile)
[<Fact(Skip = "requires test file", SkipUnless = nameof testfileExists)>]
let ``Part 1 realworld`` () =
System.IO.File.ReadAllText(testfile) |> part1 =! 156388521
[<Fact(Skip = "requires test file", SkipUnless = nameof testfileExists)>]
let ``Part 2 realworld`` () =
System.IO.File.ReadAllText(testfile) |> part2 =! 75920122
module ``FParsec Tests`` =
let test parser input =
runParserOnString parser UserState.Default "" input
|> function
| Success(result, _, _) -> Result.Ok result
| Failure(msg, _, _) -> Result.Error msg
[<Fact>]
let ``Test mul(123,456)`` () =
test mul "mul(123,456)" =! Result.Ok(123, 456)
[<Fact>]
let ``Test mul(123,456`` () =
test mul "mul(123,456]" <>! Result.Ok(123, 456)