-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
52 lines (41 loc) · 1.39 KB
/
Program.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 Day19
open System.Text.RegularExpressions
open System.IO
let createPattern rule (rules: Map<int, string>) =
let rec build r n =
match n, rules.[r] with
| n, _ when n > 25 -> "" // No rule goes this deep
| _, "\"a\"" -> "a"
| _, "\"b\"" -> "b"
| n, composite ->
composite.Split(" ")
|> Array.map (fun s -> if s = "|" then "|" else build (int s) (n + 1))
|> Array.fold (+) ""
|> sprintf "(%s)"
sprintf "^%s$" (build rule 0)
let partOne (rules: Map<int, string>) messages =
let pattern = createPattern 0 rules
messages
|> List.filter (fun m -> Regex.IsMatch(m, pattern))
|> List.length
let partTwo (rules: Map<int, string>) messages =
let newRules =
rules
|> Map.add 8 "42 | 42 8"
|> Map.add 11 "42 31 | 42 11 31"
partOne newRules messages
[<EntryPoint>]
let main argv =
let input =
File.ReadLines("Input.txt")
|> Seq.toList
let rules =
input
|> List.takeWhile (fun s -> s <> "")
|> List.map (fun s -> s.Split(": "))
|> List.map (fun s -> int s.[0], s.[1])
|> Map.ofList
let messages = input |> List.skip (Map.count rules + 1)
partOne rules messages |> printfn "Part one: %d"
partTwo rules messages |> printfn "Part two: %d"
0