-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.nix
86 lines (86 loc) · 1.95 KB
/
day4.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
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
let
inherit (builtins)
;
inherit (import <nixpkgs/lib>)
range
;
inherit (import ./mylib.nix)
_0
_1
bool_to_int
elem_at_2d
len
string_to_arr2d
sum
tensor_product
;
# lines_to_chars_arr2d = lines:
# assert all (l: eq (len l) (len lines)) lines;
# lines
# |> map string_to_list
# ;
# optimization idea: map chars to ints, bc they probably use less memory: X->0 M->1 A->2 S->3
is_xmas_or_rev = a: b: c: d: [a b c d] == ["X" "M" "A" "S"] || [a b c d] == ["S" "A" "M" "X"];
count_hor = arr2d:
tensor_product (range 0 (len arr2d - 4)) (range 0 (len arr2d - 1))
|> map (xy:
let x = _0 xy; y = _1 xy; in
is_xmas_or_rev
(elem_at_2d x y arr2d)
(elem_at_2d (x+1) y arr2d)
(elem_at_2d (x+2) y arr2d)
(elem_at_2d (x+3) y arr2d)
|> bool_to_int
)
|> sum
;
count_ver = arr2d:
tensor_product (range 0 (len arr2d - 1)) (range 0 (len arr2d - 4))
|> map (xy:
let x = _0 xy; y = _1 xy; in
is_xmas_or_rev
(elem_at_2d x y arr2d)
(elem_at_2d x (y+1) arr2d)
(elem_at_2d x (y+2) arr2d)
(elem_at_2d x (y+3) arr2d)
|> bool_to_int
)
|> sum
;
count_diag = arr2d:
tensor_product (range 0 (len arr2d - 4)) (range 0 (len arr2d - 4))
|> map (xy:
let x = _0 xy; y = _1 xy; in
is_xmas_or_rev
(elem_at_2d x y arr2d)
(elem_at_2d (x+1) (y+1) arr2d)
(elem_at_2d (x+2) (y+2) arr2d)
(elem_at_2d (x+3) (y+3) arr2d)
|> bool_to_int
)
|> sum
;
count_antidiag = arr2d:
tensor_product (range 0 (len arr2d - 4)) (range 0 (len arr2d - 4))
|> map (xy:
let x = _0 xy; y = _1 xy; in
is_xmas_or_rev
(elem_at_2d x (y+3) arr2d)
(elem_at_2d (x+1) (y+2) arr2d)
(elem_at_2d (x+2) (y+1) arr2d)
(elem_at_2d (x+3) y arr2d)
|> bool_to_int
)
|> sum
;
count_xmas = arr2d:
(count_hor arr2d) +
(count_ver arr2d) +
(count_diag arr2d) +
(count_antidiag arr2d)
;
in
input:
input
|> string_to_arr2d
|> count_xmas