-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
57 lines (44 loc) · 1.61 KB
/
Program.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
module Day17
open Common.Navigation
open System.IO
let parse (y, line) =
Seq.mapi (fun x c -> (x, y), if c = '#' then 1 else 0) line
let run (input: ('T * int) list) (findNeighbours: 'T -> 'T list) =
let mutable state = Map.ofList input
let getState point = Map.tryFind point state |> Option.defaultValue 0
for i in 0 .. 5 do
let mutable newState =
Map.toList state
|> List.map fst
|> List.collect findNeighbours
|> List.distinct
|> List.map (fun p -> p, getState p)
|> Map.ofList
Map.toList newState
|> List.iter (fun (point, value) ->
let activeNeighbours = findNeighbours point |> List.sumBy getState
let newValue =
match value, activeNeighbours with
| 1, n when n = 2 || n = 3 -> 1
| 0, 3 -> 1
| _, _ -> 0
newState <- newState.Add(point, newValue)
)
state <- newState
state |> Map.toList |> List.map snd |> List.sum
let partOne input =
let coordinates = List.map (fun ((x, y), state) -> (x, y, 0), state) input
run coordinates neighbours3D
let partTwo input =
let coordinates = List.map (fun ((x, y), state) -> (x, y, 0, 0), state) input
run coordinates neighbours4D
[<EntryPoint>]
let main argv =
let input =
File.ReadLines("Input.txt")
|> Seq.indexed
|> Seq.collect parse
|> Seq.toList
partOne input |> printfn "Part one: %d"
partTwo input |> printfn "Part two: %d"
0