-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.fut
73 lines (63 loc) · 2.01 KB
/
24.fut
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
-- Absolutely delightful. A proper BFS might be more efficient, but
-- the stencil works nicely too.
import "utils"
def parse s =
let (get,ls) = lines.lines s
let m = length (get ls[0]) - 2
let on_line l = get l |> drop 1 |> take m
in map on_line ls[1:length ls-1]
def easts wind = map (map (u8.=='>')) wind
def wests wind = map (map (u8.=='<')) wind
def norths wind = map (map (u8.=='^')) wind
def souths wind = map (map (u8.=='v')) wind
-- Windy cells after i steps.
def windy_in (i: i64) (wind: [][]u8) =
map4 (map4 (\a b c d -> a||b||c||d))
(map (rotate (-i)) (easts wind))
(map (rotate i) (wests wind))
(rotate i (norths wind))
(rotate (-i) (souths wind))
#[noinline]
def step [n][m] (start: (i64,i64)) (wind: [n][m]u8) (s: i64) (where: [n][m]bool): [n][m]bool =
let present i j = (i,j) == start || (i >= 0 && i < n && j >= 0 && j < m && where[i,j])
let windy = windy_in s wind
in tabulate_2d n m
(\i j -> !windy[i,j] &&
(present i j || present (i-1) j || present (i+1) j
|| present i (j-1) || present i (j+1)))
entry part1 s =
let [n][m] wind : [n][m]u8 = parse s
let go = true
let s = 1
let where = replicate n (replicate m false)
let (_,s,_) =
loop (go,s,where) while go do
let where = step (-1,0) wind s where
in (!where[n-1,m-1], s+1, where)
in s
entry part2 s =
let [n][m] wind : [n][m]u8 = parse s
let go = true
let s = 1
let where = replicate n (replicate m false)
let (_,s,_) =
loop (go,s,where) while go do
let where = step (-1,0) wind s where
in (!where[n-1,m-1], s+1, where)
let (_,s,_) =
loop (go,s,where) while go do
let where = step (n,m-1) wind s where
in (!where[0,0], s+1, where)
let (_,s,_) =
loop (go,s,where) while go do
let where = step (-1,0) wind s where
in (!where[n-1,m-1], s+1, where)
in s
-- ==
-- entry: part1
-- input @ data/24.input
-- output { 373i64 }
-- ==
-- entry: part2
-- input @ data/24.input
-- output { 997i64 }