-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.fs
67 lines (59 loc) · 2.18 KB
/
08.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
open System
open System.IO
open System.Numerics
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let DEBUG = false
let dbg v =
if DEBUG then
printfn "%A" v
v
let lines = File.ReadAllLines(@"input")
let ans1 =
(lines.Length, lines[0].Length)
|> (fun (N, M) ->
lines
|> Array.mapi (fun x r -> r.ToCharArray() |> Array.mapi (fun y v -> (v, (x, y))))
|> Array.collect id
|> Array.groupBy fst
|> Array.filter (fun (c, a) -> c <> '.')
|> Array.map (fun (c, a) ->
Array.allPairs a a
|> Array.filter (fun (ai, aj) -> ai <> aj)
|> Array.map (fun ((_, (x1, y1)), (_, (x2, y2))) ->
[| (x1 + 2 * (x2 - x1), y1 + 2 * (y2 - y1))
(x2 + 2 * (x1 - x2), y2 + 2 * (y1 - y2)) |]
|> Array.filter (fun (a, b) -> a >= 0 && a < N && b >= 0 && b < M))
|> Array.collect id))
|> Array.collect id
|> Array.groupBy id
|> Array.length
let ans2 =
(lines.Length, lines[0].Length)
|> (fun (N, M) ->
lines
|> Array.mapi (fun x r -> r.ToCharArray() |> Array.mapi (fun y v -> (v, (x, y))))
|> Array.collect id
|> Array.groupBy fst
|> Array.filter (fun (c, a) -> c <> '.')
|> Array.map (fun (c, a) ->
Array.allPairs a a
|> Array.filter (fun (ai, aj) -> ai <> aj)
|> Array.map (fun ((_, (x1, y1)), (_, (x2, y2))) ->
(1
|> Array.unfold (fun i ->
match (x1 + i * (x2 - x1), y1 + i * (y2 - y1)) with
| (a, b) when a >= 0 && a < N && b >= 0 && b < M -> Some((a, b), i + 1)
| _ -> None),
1
|> Array.unfold (fun i ->
match (x2 + i * (x1 - x2), y2 + i * (y1 - y2)) with
| (a, b) when a >= 0 && a < N && b >= 0 && b < M -> Some((a, b), i + 1)
| _ -> None))
||> Array.append)
|> Array.collect id))
|> Array.collect id
|> Array.groupBy id
|> Array.length
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds