-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18_part02.fs
63 lines (49 loc) · 1.93 KB
/
day18_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module day18_part02
open AdventOfCode_2016.Modules
open System.Collections.Generic
type TileKind =
| SAFE
| TRAP
let parseContent (content: string) =
content.ToCharArray()
|> Array.map(fun c -> if c = '.' then SAFE else TRAP)
let isTrap((left, center, right): (TileKind*TileKind*TileKind)) =
match (left, center, right) with
| (l, c, r) when l.IsTRAP && c.IsTRAP && r.IsSAFE -> true
| (l, c, r) when l.IsSAFE && c.IsTRAP && r.IsTRAP -> true
| (l, c, r) when l.IsTRAP && c.IsSAFE && r.IsSAFE -> true
| (l, c, r) when l.IsSAFE && c.IsSAFE && r.IsTRAP -> true
| _ -> false
let nextState (floor: TileKind array) =
let floorchecker = Array.concat[[|SAFE|]; floor; [|SAFE|]]
floor
|> Array.mapi(fun idx _ ->
if isTrap (floorchecker[idx-1+1], floorchecker[idx+1], floorchecker[idx+1+1]) then TRAP else SAFE
)
let printState(floor: TileKind array) =
String.concat "" (floor |> Array.map(fun t -> if t.IsSAFE then "." else "^"))
let countSafes(floor: TileKind array) =
floor |> Array.filter _.IsSAFE |> Array.length
let walk (floor: TileKind array) (rows: int) (memo: Dictionary<TileKind array, TileKind array>)=
let mutable currentRow = 1
let mutable currentCount = 0
let mutable newstate = floor
currentCount <- currentCount + countSafes newstate
while currentRow < rows do
newstate <-
if memo.ContainsKey(newstate) then
memo[newstate]
else
let t' = nextState newstate
memo.Add(newstate, t')
t'
currentCount <- currentCount + countSafes newstate
currentRow <- currentRow + 1
currentCount
let execute =
let path = "day18/day18_input.txt"
let content = LocalHelper.GetContentFromFile path
let floor = parseContent content
let mapSize = 400000
let memo = Dictionary<TileKind array, TileKind array>()
walk floor mapSize memo