-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.fs
123 lines (114 loc) · 5.09 KB
/
24.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
open System
open System.IO
open System.Collections.Generic
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = true
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines =
File.ReadAllText(@"input").Trim().Split("\n\n")
|> fun x ->
(x[0].Split("\n")
|> Array.map (fun y -> y.Split(": ") |> fun z -> (z[0], z[1] |> int)),
x[1].Split("\n")
|> Array.map (fun y ->
y.Split(" -> ")
|> fun z -> (z[0].Split(" ") |> fun t -> (t[0], t[1], t[2]), z[1])))
let ans1 =
lines
|> fun (a, b) ->
(a |> dict |> Dictionary, b)
||> fun v c ->
[ 0 .. (c.Length - 1) ]
|> Array.unfold (fun q ->
match q.Length with
| 0 -> None
| _ ->
Some(
0,
q
|> List.filter (fun i ->
match c[i] with
| ((a, g, b), t) ->
match v.ContainsKey a && v.ContainsKey b with
| true ->
match g with
| "AND" -> v.Add(t, v[a] &&& v[b]) <> ()
| "OR" -> v.Add(t, v[a] ||| v[b]) <> ()
| _ -> v.Add(t, v[a] ^^^ v[b]) <> ()
| false -> true)
))
|> fun _ ->
0
|> Array.unfold (fun i ->
match "z" + (i |> string).PadLeft(2, '0') with
| z when v.ContainsKey z -> Some(v[z] |> int64, i + 1)
| _ -> None)
|> Array.mapi (fun i x -> x <<< i)
|> Array.sum
let ans2 =
lines
|> fun (a, b) ->
(a |> dict |> Dictionary, b)
||> fun v c ->
(0
|> Array.unfold (fun i ->
match "x" + (i |> string).PadLeft(2, '0') with
| x when v.ContainsKey x -> Some(v[x], i + 1)
| _ -> None),
0
|> Array.unfold (fun i ->
match "y" + (i |> string).PadLeft(2, '0') with
| x when v.ContainsKey x -> Some(v[x], i + 1)
| _ -> None))
||> fun x y ->
(0, [ 0 .. (x.Length - 1) ])
||> List.mapFold (fun c i -> (x[i] ^^^ y[i] ^^^ c, (x[i] &&& y[i]) ||| (x[i] &&& c) ||| (y[i] &&& c)))
||> fun a b -> a @ [ b ]
|> List.mapi (fun i x -> ("z" + (i |> string).PadLeft(2, '0'), x))
|> Map
|> fun M ->
(0, [ 0 .. (c.Length - 1) ], Dictionary<string, (string * string * string)>())
|> Array.unfold (fun (k, q, s) ->
match q.Length with
| 0 -> None
| _ ->
match
q
|> List.filter (fun i ->
match c[i] with
| ((a, g, b), t) ->
match
(((not (a.StartsWith("x"))) && (not (a.StartsWith("y"))))
|| (a.Substring(1) |> int) <= k)
&& (((not (a.StartsWith("x"))) && (not (a.StartsWith("y"))))
|| (b.Substring(1) |> int) <= k)
&& v.ContainsKey a
&& v.ContainsKey b
with
| true ->
match
match g with
| "AND" -> v[a] &&& v[b]
| "OR" -> v[a] ||| v[b]
| _ -> v[a] ^^^ v[b]
with
| r ->
match t.StartsWith("z") && r <> M[t] with
| true ->
v.Add(t, r) <> s.Add(t, (a, g, b))
|> fun _ ->
("\n\n\n\n\n\n\n\n\n", (a, g, b), t, s) |> dbg |> (fun _ -> false)
| false ->
v.Add(t, r) <> s.Add(t, (a, g, b))
|> fun _ -> ((a, g, b), t, s) |> dbg |> (fun _ -> false)
| false -> true)
with
| l when l.Length = q.Length ->
Some(0, (k + 1, q, Dictionary<string, (string * string * string)>()))
| l -> Some(0, (k, l, s)))
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds