-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09_part01.fs
34 lines (26 loc) · 1.38 KB
/
day09_part01.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
module day09_part01
open System.IO
let path = "day09_input.txt"
//let path = "test_input.txt"
//let path = "test_input_00.txt"
let inputPartsCollection =
File.ReadLines(__SOURCE_DIRECTORY__ + @"../../" + path) |> Seq.map(fun l -> l.ToCharArray() |> Array.map string |> Array.map int) |> Seq.toArray
let columns = inputPartsCollection.[0].Length
let rows = inputPartsCollection.Length
let topbottomArray = [|Array.init(columns + 2)(fun i -> 9)|]
let adaptedinput = inputPartsCollection |> Array.map(fun r -> Array.concat[[|9|]; r; [|9|]])
let paddedArray = Array.concat([topbottomArray; adaptedinput; topbottomArray])
let valuesArray = Array2D.zeroCreate<int> (rows) (columns)
let isSmallest(row: int, col: int, values:int[][]) =
let valueToCheck = values.[row].[col]
let neighboors = [values.[row - 1].[col]; values.[row].[col - 1]; values.[row].[col + 1]; values.[row + 1].[col]]
neighboors |> List.forall(fun n -> valueToCheck < n)
let execute =
[1 .. rows]
|> Seq.iter(fun i -> [1 .. columns] |> Seq.iter(fun j -> valuesArray[i - 1, j - 1] <- -1)
)
[1 .. rows]
|> Seq.iter(fun i -> [1 .. columns] |> Seq.iter(fun j -> if isSmallest(i, j, paddedArray) then valuesArray[i - 1, j - 1] <- paddedArray.[i].[j])
)
let elements = valuesArray |> Seq.cast<int> |> Seq.filter (fun x -> x <> -1) |> Seq.toArray
(elements |> Seq.sum) + elements.Length