-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.fs
99 lines (91 loc) · 3.38 KB
/
10.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
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.ReadAllLines(@"input")
|> Array.map Array.ofSeq
|> Array.map (Array.map (fun x -> x |> Char.GetNumericValue |> int))
let ans1 =
(lines, lines.Length, lines[0].Length)
|||> (fun l n m ->
l
|> Array.mapi (fun i r ->
r
|> Array.mapi (fun j v ->
match v with
| 0 -> (i, j)
| _ -> (-1, -1)))
|> Array.collect id
|> Array.filter ((<>) (-1, -1))
|> Array.sumBy (fun (X, Y) ->
[ (X, Y, 0) ]
|> Array.unfold (fun s ->
match s.Length with
| 0 -> None
| _ ->
match s[0] with
| (x, y, 9) -> Some((x, y), List.tail s)
| (x, y, i) ->
Some(
(-1, -1),
([ (x + 1, y, i + 1); (x - 1, y, i + 1); (x, y + 1, i + 1); (x, y - 1, i + 1) ]
|> List.filter (fun (x, y, i) ->
match x with
| x when x < 0 || x >= n -> false
| _ ->
match y with
| y when y < 0 || y >= m -> false
| _ ->
match l[x][y] with
| j when j = i -> true
| _ -> false))
@ (List.tail s)
))
|> Array.distinct
|> Array.filter ((<>) (-1, -1))
|> Array.length))
let ans2 =
(lines, lines.Length, lines[0].Length)
|||> (fun l n m ->
l
|> Array.mapi (fun i r ->
r
|> Array.mapi (fun j v ->
match v with
| 0 -> (i, j)
| _ -> (-1, -1)))
|> Array.collect id
|> Array.filter ((<>) (-1, -1))
|> Array.sumBy (fun (X, Y) ->
[ (X, Y, 0) ]
|> Array.unfold (fun s ->
match s.Length with
| 0 -> None
| _ ->
match s[0] with
| (x, y, 9) -> Some(1, List.tail s)
| (x, y, i) ->
Some(
0,
([ (x + 1, y, i + 1); (x - 1, y, i + 1); (x, y + 1, i + 1); (x, y - 1, i + 1) ]
|> List.filter (fun (x, y, i) ->
match x with
| x when x < 0 || x >= n -> false
| _ ->
match y with
| y when y < 0 || y >= m -> false
| _ ->
match l[x][y] with
| j when j = i -> true
| _ -> false))
@ (List.tail s)
))
|> Array.sum))
stopWatch.Stop()
printfn "%A %A" ans1 ans2
printfn "Time: %fms" stopWatch.Elapsed.TotalMilliseconds