-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06_part02.fs
48 lines (41 loc) · 1.77 KB
/
day06_part02.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
module day06_part02
open System.IO
open System
open System.Collections.Generic
let mDistance (origin: int[]) (dest: int[]) =
Math.Abs(origin.[0] - dest.[0]) + Math.Abs(origin.[1] - dest.[1])
let getShortestDistance (point: int[]) (lCords: int array list) =
let distances = lCords |> List.map(fun c -> (c.[0], mDistance point [|c.[1]; c.[2]|])) // check * distance
distances |> List.sumBy(fun c -> snd c)
let createPanel size =
let dict = new Dictionary<string, int>()
for colIdx in 0..size-1 do
for rowIdx in 0..size-1 do
dict.Add($"{rowIdx},{colIdx}", 0)
dict
let calculateDistances (size: int) (coords: int array list) (panel: Dictionary<string, int>) =
for colIdx in 0..size-1 do
for rowIdx in 0..size-1 do
let distances = getShortestDistance [|rowIdx; colIdx|] coords
panel.Item($"{rowIdx},{colIdx}") <- distances
let count (size: int) (maxvalue: int) (panel: Dictionary<string, int>) =
let result =
seq {
for colIdx in 0..size-1 do
for rowIdx in 0..size-1 do
let value = panel.Item($"{rowIdx},{colIdx}")
if value < maxvalue then
yield value
} |> Seq.length
result
let execute =
let filepath = __SOURCE_DIRECTORY__ + @"./../day06_input.txt"
let inputLines = File.ReadAllLines(filepath) |> List.ofArray
let coords = inputLines |> List.mapi(fun idx v -> [|idx; int(v.Split(", ")[0]); int(v.Split(", ")[1]); 0|])
let size = 400
let maxDistance = 10000
let panel = createPanel size
let counter = Array.create coords.Length 0
coords |> List.iter(fun e -> panel.Item($"{e.[1]},{e.[2]}") <- e.[0])
calculateDistances size coords panel
count size maxDistance panel