-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16_part01.fs
52 lines (43 loc) · 1.51 KB
/
day16_part01.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
module day16_part01
open System.Text.RegularExpressions
open AdventOfCode_2020.Modules
type RangeKind =
| Class
| Row
| Seat
type Range = {
Start: int
End: int
}
type Notes = {
Ranges: Range list
MyTickets: int array
OtherTickets: int array
}
let parseContent(input: string) =
let parts = input.Split($"{System.Environment.NewLine}{System.Environment.NewLine}")
let rangepattern = @"(\d+)-(\d+) or (\d+)-(\d+)"
let numberspattern = @"(\d+)"
let ranges =
[
for p in parts[0].Split(System.Environment.NewLine) do
let m' = Regex.Match(p, rangepattern)
let (f1, t1, f2, t2) =
((int)(m'.Groups[1].Value), (int)(m'.Groups[2].Value), (int)(m'.Groups[3].Value), (int)(m'.Groups[4].Value))
yield! [{ Start = f1; End = t1 }; { Start = f2; End = t2 }]
]
let myticket = (parts[1].Split(System.Environment.NewLine)[1]).Split(",") |> Array.map int
let o' = Regex.Matches((parts[2]), numberspattern)
let othertickets = o' |> Seq.map(fun v -> int(v.Value))
{ Ranges = ranges; MyTickets = myticket; OtherTickets = othertickets |> Array.ofSeq }
let errorRate(notes: Notes) =
notes.OtherTickets
|> Array.filter(fun t ->
not (notes.Ranges |> List.exists(fun r -> r.Start <= t && t <= r.End))
)
|> Array.sum
let execute =
let path = "day16/day16_input.txt"
let content = LocalHelper.GetContentFromFile path
let notes = parseContent content
errorRate notes