-
Notifications
You must be signed in to change notification settings - Fork 0
/
20.fs
119 lines (108 loc) · 4.22 KB
/
20.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
open System
open System.IO
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines = File.ReadAllText(@"input").Trim().Split("\n") |> Array.map Array.ofSeq
let ans1 =
(lines, lines.Length, lines[0].Length)
|||> fun l n m ->
Array.allPairs [| 0 .. (n - 1) |] [| 0 .. (m - 1) |]
|> Array.find (fun (x, y) ->
match l[x][y] with
| 'S' -> true
| _ -> false)
|> fun (sx, sy) -> (sx, sy, sx, sy)
|> Array.unfold (fun (x, y, bx, by) ->
match (x, y) with
| (-1, -1) -> None
| _ ->
match l[x][y] with
| 'E' -> Some((x, y), (-1, -1, -1, -1))
| _ ->
Some(
(x, y),
([ (0, 1); (0, -1); (1, 0); (-1, 0) ]
|> List.find (fun (dx, dy) ->
match x + dx with
| x when x < 0 || x >= n -> false
| _ ->
match y + dy with
| y when y < 0 || y >= m -> false
| _ ->
match (x + dx, y + dy) with
| p when p = (bx, by) -> false
| _ ->
match l[x + dx][y + dy] with
| '#' -> false
| _ -> true)
|> fun (dx, dy) -> (x + dx, y + dy, x, y))
))
|> fun r ->
r
|> Array.Parallel.mapi (fun i (x0, y0) ->
r
|> Array.mapi (fun j (x1, y1) ->
match j with
| j when j > i ->
match (pown (x0 - x1) 2) + (pown (y0 - y1) 2) with
| 4 -> j - i - 2
| _ -> 0
| _ -> 0)
|> Array.filter (fun x -> x >= 100)
|> Array.length)
|> Array.sum
let ans2 =
(lines, lines.Length, lines[0].Length)
|||> fun l n m ->
Array.allPairs [| 0 .. (n - 1) |] [| 0 .. (m - 1) |]
|> Array.find (fun (x, y) ->
match l[x][y] with
| 'S' -> true
| _ -> false)
|> fun (sx, sy) -> (sx, sy, sx, sy)
|> Array.unfold (fun (x, y, bx, by) ->
match (x, y) with
| (-1, -1) -> None
| _ ->
match l[x][y] with
| 'E' -> Some((x, y), (-1, -1, -1, -1))
| _ ->
Some(
(x, y),
([ (0, 1); (0, -1); (1, 0); (-1, 0) ]
|> List.find (fun (dx, dy) ->
match x + dx with
| x when x < 0 || x >= n -> false
| _ ->
match y + dy with
| y when y < 0 || y >= m -> false
| _ ->
match (x + dx, y + dy) with
| p when p = (bx, by) -> false
| _ ->
match l[x + dx][y + dy] with
| '#' -> false
| _ -> true)
|> fun (dx, dy) -> (x + dx, y + dy, x, y))
))
|> fun r ->
r
|> Array.Parallel.mapi (fun i (x0, y0) ->
r
|> Array.mapi (fun j (x1, y1) ->
match j with
| j when j > i ->
match abs (x0 - x1) + abs (y0 - y1) with
| x when x <= 20 -> j - i - x
| _ -> 0
| _ -> 0)
|> Array.filter (fun x -> x >= 100)
|> Array.length)
|> Array.sum
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds