-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day03_noregex.fs
92 lines (73 loc) · 2.83 KB
/
Day03_noregex.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
92
module aoc24.Day03noregex
open System
/// Matches a single digit char and returns it's int value
let (|Digit|_|) =
function
| c when Char.IsAsciiDigit c -> Some(int c - int '0')
| _ -> None
/// matches a 1-3 digit number with a delimiter, for example '123,' or '2)'
let (|Number|_|) delim =
function
| Digit a :: Digit b :: Digit c :: x :: tail when x = delim -> Some(a * 100 + b * 10 + c, tail)
| Digit a :: Digit b :: x :: tail when x = delim -> Some(a * 10 + b, tail)
| Digit a :: x :: tail when x = delim -> Some(a, tail)
| _ -> None
/// matches a 'mul(a,b)' and returns a, b and the remaining characters after
let (|Mul|_|) =
function
| 'm' :: 'u' :: 'l' :: '(' :: Number ',' (a, tail) ->
match tail with
| Number ')' (b, tail) -> Some(a, b, tail)
| _ -> None
| _ -> None
let mulSums = Seq.sumBy (TupleEx.apply (*))
let part1 input =
let rec parse output =
function
| Mul(a, b, tail) -> parse ((a, b) :: output) tail
| _ :: tail -> parse output tail
| [] -> output
let muls = input |> List.ofSeq |> parse []
muls |> mulSums
/// matches 'do()' and returns the remaining characters after
let (|Do|_|) =
function
| 'd' :: 'o' :: '(' :: ')' :: tail -> Some tail
| _ -> None
/// matches 'don't()' and returns the remaining characters after
let (|Dont|_|) =
function
| 'd' :: 'o' :: 'n' :: ''' :: 't' :: '(' :: ')' :: tail -> Some tail
| _ -> None
type DoState =
| Enabled
| Disabled
let part2 input =
let rec parse output doState =
function
| Do tail -> parse output Enabled tail
| Dont tail -> parse output Disabled tail
| Mul(a, b, tail) when doState.IsEnabled -> parse ((a, b) :: output) doState tail
| _ :: tail -> parse output doState tail
| [] -> output
let muls = input |> List.ofSeq |> parse [] Enabled
muls |> mulSums
module tests =
open Swensen.Unquote
open Xunit
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