-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10_part2.nix
57 lines (57 loc) · 910 Bytes
/
day10_part2.nix
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
let
inherit (builtins)
all
filter
isList
length
trace
;
inherit (import <nixpkgs/lib>)
flatten
sublist
toInt
unique
;
inherit (import ./mylib.nix)
_0
_1
arr2d_is_in_0square
elem_at_2d
eq
find_indices_in_arr2d
map_rec
ne
string_to_arr2d
sum
;
trailhead_rating_ = m: s: yx: prev_h:
let
y = _0 yx;
x = _1 yx;
h = elem_at_2d x y m;
in
if arr2d_is_in_0square s x y && h - prev_h == 1 then
if h == 9 then
{y=y; x=x;}
else
[ [y (x + 1)] [y (x - 1)] [(y + 1) x] [(y - 1) x] ]
|> map (yx: trailhead_rating_ m s yx h)
|> filter (ne null)
else
null
;
trailhead_rating = m: s: yx:
trailhead_rating_ m s yx (-1)
|> flatten
|> length
;
in
input:
let
m = input |> string_to_arr2d |> map_rec toInt;
s = length m;
thyxs = m |> find_indices_in_arr2d (eq 0);
in
thyxs
|> map (trailhead_rating m s)
|> sum