-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4_part2.nix
55 lines (55 loc) · 877 Bytes
/
day4_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
let
inherit (builtins)
any
;
inherit (import <nixpkgs/lib>)
range
;
inherit (import ./mylib.nix)
_0
_1
bool_to_int
elem_at_2d
eq
id
juxt
len
shift_l
shift_r
string_to_arr2d
sum
tensor_product
;
MMSS = ["M" "M" "S" "S"];
is_x_mas = a: b: c: d: e:
if a != "A" then
false
else
any
(eq MMSS)
(juxt [b c d e] [
id
(shift_l 1)
(shift_l 2)
(shift_r 1)
])
;
count_x_mas = arr2d:
tensor_product (range 1 (len arr2d - 2)) (range 1 (len arr2d - 2))
|> map (xy:
let x = _0 xy; y = _1 xy; in
is_x_mas
(elem_at_2d x y arr2d)
(elem_at_2d (x + 1) (y + 1) arr2d)
(elem_at_2d (x + 1) (y - 1) arr2d)
(elem_at_2d (x - 1) (y - 1) arr2d)
(elem_at_2d (x - 1) (y + 1) arr2d)
|> bool_to_int
)
|> sum
;
in
input:
input
|> string_to_arr2d
|> count_x_mas