This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.fsx
More file actions
75 lines (62 loc) · 2.03 KB
/
Day8.fsx
File metadata and controls
75 lines (62 loc) · 2.03 KB
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
#r "nuget: FParsec"
open FParsec
open System
type Parser<'t> = Parser<'t, unit>
let parse p str =
match run p str with
| Success (result, _, _) -> result
| Failure (errorMsg, _, _) -> failwith errorMsg
let input =
IO.File.ReadLines "Day8.txt"
|> Seq.map (fun s ->
s
|> Seq.map(fun char -> int char - int '0')
|> Array.ofSeq)
|> Array.ofSeq
|> array2D
let removeInvalidIndices =
List.filter (fun (x, y) -> x >= 0 && y >= 0 && x < Array2D.length1 input && y < Array2D.length2 input)
let getNeighborhood (xPos, yPos) l =
l
|> List.map(fun (x, y) -> xPos + x, yPos + y)
|> removeInvalidIndices
let getMooreHood (xPos, yPos) =
List.allPairs [-1; 0; 1] [-1; 0; 1]
|> List.map (fun (x, y) -> xPos + x, yPos + y)
|> removeInvalidIndices
let cardinalNeighbors (xPos, yPos) =
[(1, 0); (-1, 0); (0, 1); (0, -1)]
|> List.map (fun (x, y) -> xPos + x, yPos + y)
|> removeInvalidIndices
let getMin l =
l |> List.reduce (fun hd acc -> if hd < acc then hd else acc)
let lowPoints =
List.allPairs [0 .. (Array2D.length1 input) - 1] [0 .. (Array2D.length2) input - 1]
|> List.filter(fun (x, y) ->
input.[x, y] = (getMooreHood (x, y) |> List.map(fun (x, y) -> input.[x, y]) |> getMin))
let problem1 =
lowPoints
|> List.map(fun (x, y) -> input.[x, y] + 1)
|> List.sum
let findBasin (lowpoint : int * int) =
let rec loop (toCheck : (int * int) list) (seen : (int * int) list) =
match toCheck with
| [] -> seen
| hd::tl ->
if List.contains hd seen then
loop tl seen
else
let toAdd =
hd
|> cardinalNeighbors
|> List.filter(fun (x, y) -> input.[x, y] <> 9)
loop (List.append toAdd toCheck) (hd::seen)
loop [lowpoint] []
let problem2 =
lowPoints
|> List.map(fun lowPoint ->
findBasin lowPoint
|> List.length)
|> List.sortDescending
|> List.take 3
|> List.reduce(*)