-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.fs
224 lines (174 loc) · 5.22 KB
/
Day11.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
module advent_of_code_2021.Day11
open System.IO
open System.Reflection.Emit
open Swensen.Unquote
open Xunit
type OctopusMap = int [,]
module OctopusMap =
let parse (lines: string list) : OctopusMap =
let cols = lines.[0].Length
let rows = lines.Length
Array2D.init cols rows (fun x y -> lines.[y].[x] |> string |> int)
let toString (map: OctopusMap) =
seq {
for y in 0 .. ((map |> Array2D.length1) - 1) do
yield
new string (
map.[*, y]
|> Array.map (fun x -> (('0' |> int) + x) |> char)
)
}
|> Seq.toList
let find predicate (map: OctopusMap) =
seq {
for x in 0 .. ((map |> Array2D.length1) - 1) do
for y in 0 .. ((map |> Array2D.length2) - 1) do
if predicate map.[x, y] then
yield (x, y)
}
let findFlashers = find (fun x -> x > 9)
let adjacentPositions (x, y) (map: OctopusMap) =
let inBounds (x, y) =
x >= 0
&& y >= 0
&& x < Array2D.length1 map
&& y < Array2D.length2 map
let getWithOffset (offX, offY) =
match (x + offX, y + offY) with
| px, py when inBounds (px, py) -> Some(px, py)
| _ -> None
[ -1 .. 1 ]
|> List.allPairs [ -1 .. 1 ]
|> List.except [ (0, 0) ]
|> List.choose getWithOffset
let step (map: OctopusMap) =
let newMap = map |> Array2D.map ((+) 1)
let rec getFlashCount alreadyFlashed flashCount =
let newFlashers =
newMap
|> findFlashers
|> Seq.except alreadyFlashed
|> List.ofSeq
newFlashers
|> List.collect (fun x -> adjacentPositions x map)
|> List.iter (fun (x, y) -> newMap.[x, y] <- (newMap.[x, y] + 1))
match newFlashers with
| [] ->
(newMap
|> Array2D.map (fun i -> if i <= 9 then i else 0),
flashCount)
| _ -> getFlashCount (alreadyFlashed @ newFlashers) (flashCount + newFlashers.Length)
getFlashCount [] 0
let part1 input =
let map = input |> OctopusMap.parse
let folder (state, flashCount) _ =
let newMap, newFlashes = state |> OctopusMap.step
(newMap, flashCount + newFlashes)
[ 1 .. 100 ] |> List.fold folder (map, 0) |> snd
let part2 input =
let mutable map = input |> OctopusMap.parse
let isZero map =
map
|> Seq.cast<int>
|> Seq.forall (fun x -> x = 0)
let rec solve state i =
let nextState, _ = state |> OctopusMap.step
if isZero nextState then
i + 1
else
solve nextState i + 1
solve map 0
let run () =
let input =
File.ReadAllLines("inputs/Day11.txt")
|> List.ofArray
$"{part1 input} {part2 input}"
module test =
let testData =
[ "5483143223"
"2745854711"
"5264556173"
"6141336146"
"6357385478"
"4167524645"
"2176841721"
"6882881134"
"4846848554"
"5283751526" ]
let step1 =
[ "6594254334"
"3856965822"
"6375667284"
"7252447257"
"7468496589"
"5278635756"
"3287952832"
"7993992245"
"5957959665"
"6394862637" ]
let step2 =
[ "8807476555"
"5089087054"
"8597889608"
"8485769600"
"8700908800"
"6600088989"
"6800005943"
"0000007456"
"9000000876"
"8700006848" ]
let parsed = testData |> OctopusMap.parse
[<Fact>]
let ```parse and tostring`` () =
test
<@ (testData
|> OctopusMap.parse
|> OctopusMap.toString) = testData @>
[<Fact>]
let part1 () = test <@ part1 testData = 1656 @>
[<Fact>]
let part2 () = test <@ part2 testData = 195 @>
[<Fact>]
let ``simpleExample`` () =
let initial =
[ "11111"
"19991"
"19191"
"19991"
"11111" ]
let step1 =
[ "34543"
"40004"
"50005"
"40004"
"34543" ]
let step2 =
[ "45654"
"51115"
"61116"
"51115"
"45654" ]
let after1 =
initial
|> OctopusMap.parse
|> OctopusMap.step
|> fst
test <@ (after1 |> OctopusMap.toString) = step1 @>
let after2 = after1 |> OctopusMap.step |> fst
test <@ (after2 |> OctopusMap.toString) = step2 @>
[<Fact>]
let ``after step 1`` () =
test
<@ (parsed
|> OctopusMap.step
|> fst
|> OctopusMap.toString) = step1 @>
[<Fact>]
let ``after step 2`` () =
test
<@ (parsed
|> OctopusMap.step
|> fst
|> OctopusMap.step
|> fst
|> OctopusMap.toString) = step2 @>