-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.fs
65 lines (57 loc) · 1.76 KB
/
05.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
open System
open System.IO
open System.Text.RegularExpressions
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines =
File.ReadAllText(@"input").Trim().Split("\n\n")
|> fun x ->
(x.[0], x.[1])
||> fun a b ->
(a.Split "\n"
|> Array.map (fun x -> x.Split "|" |> Array.map int)
|> Array.countBy id
|> Map.ofSeq,
b.Split "\n" |> Array.map (fun x -> x.Split "," |> Array.map int))
let ans1 =
lines
||> fun A B ->
B
|> Array.sumBy (fun l ->
l
|> Array.mapi (fun i x ->
[| 0 .. (i - 1) |]
|> Array.forall (fun j ->
match (A |> Map.tryFind [| l.[i]; l.[j] |] |> Option.defaultValue 0) with
| 1 -> false
| _ -> true))
|> Array.forall id
|> (fun x ->
match x with
| true -> l[(l |> Array.length) / 2]
| _ -> 0))
let ans2 =
(lines
||> fun A B ->
B
|> Array.sumBy (fun l ->
l
|> Array.mapi (fun i x ->
[| 0 .. (i - 1) |]
|> Array.forall (fun j ->
match (A |> Map.tryFind [| l.[i]; l.[j] |] |> Option.defaultValue 0) with
| 1 ->
((l.[i] <- l.[i] + l.[j]) = ())
&& ((l.[j] <- l.[i] - l.[j]) = ())
&& ((l.[i] <- l.[i] - l.[j]) = ())
| _ -> true))
|> Array.forall id
|> (fun x ->
match x with
| true -> l[(l |> Array.length) / 2]
| _ -> 0)))
- ans1
printfn "%A %A" ans1 ans2