This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.fsx
More file actions
62 lines (53 loc) · 1.33 KB
/
Day10.fsx
File metadata and controls
62 lines (53 loc) · 1.33 KB
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
#r "nuget: FParsec"
open System
let input =
IO.File.ReadLines "Day10.txt"
|> Seq.map List.ofSeq
let openBrackets = ['('; '['; '<'; '{'] |> Set.ofList
let matchBracket c =
match c with
| '(' -> ')'
| '[' -> ']'
| '<' -> '>'
| '{' -> '}'
| _ -> failwith "Not an open bracket"
let scoreCloseBracket c =
match c with
| ')' -> 3L
| ']' -> 57L
| '>' -> 1197L
| '}' -> 25137L
| _ -> failwith "Not a close bracket"
type BadLine =
| Error of char
| Incomplete of char list
| Good
let parseLine originalLine =
let rec loop (openers : char list) (line : char list) =
match line with
| hd :: tl ->
if Set.contains hd openBrackets then
loop (hd :: openers) tl
else
match openers with
| hd' :: tl' ->
if matchBracket hd' = hd then
loop tl' tl
else
Error hd
| _ ->
Error hd
| _ ->
match openers with
| [] -> Good
| _ -> Incomplete openers
loop [] originalLine
let parsed =
input |> Seq.map parseLine
let problem1 =
parsed
|> Seq.choose (fun x ->
match x with
| Error c -> Some c
| _ -> None)
|> Seq.sumBy scoreCloseBracket