-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.fut
More file actions
59 lines (52 loc) · 1.57 KB
/
11.fut
File metadata and controls
59 lines (52 loc) · 1.57 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
import "util/aoc"
-- ==
-- entry: part1
-- input @ datasets/11.example.in output { 1656i32 }
-- input @ datasets/11.in output { 1741i32 }
-- ==
-- entry: part2
-- input @ datasets/11.example.in output { 195i32 }
-- input @ datasets/11.in output { 440i32 }
let parse (input: []u8) =
split_regular_lines input
|> map (map (\c -> c - '0'))
|> map (map i32.u8)
let convolve [n] [m] (oc: [n][m]i32) =
let f i j = if i < 0 || i >= n || j < 0 || j >= m then 0 else i32.bool (oc[i, j] > 9)
in tabulate_2d
n
m
(\i j ->
let x = oc[i, j]
in if x == 0 || x > 9 then 0 else
f (i - 1) (j - 1)
+ f (i - 1) j
+ f (i - 1) (j + 1)
+ f i (j - 1)
+ x
+ f i (j + 1)
+ f (i + 1) (j - 1)
+ f (i + 1) j
+ f (i + 1) (j + 1))
let flash [n] [m] (oc: [n][m]i32) =
oc
|> map (map (+1))
|> iterate_while
(\oc -> flatten oc |> any (>9))
convolve
entry part1 (input: []u8) =
(iterate
100
(\(oc, total) ->
let oc = flash oc
let count = oc |> flatten |> map (==0) |> map i32.bool |> i32.sum
in (oc, count + total))
(parse input, 0)).1
entry part2 (input: []u8) =
let oc = parse input
let (x, _) =
loop (i, oc) = (0i32, oc) while i >= 0 do
let oc = flash oc
let same = oc |> flatten |> map (== oc[0, 0]) |> reduce (&&) true
in if same then (-i - 1, oc) else (i + 1, oc)
in -x