-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.nix
45 lines (45 loc) · 881 Bytes
/
day8.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
let
inherit (builtins)
filter
length
;
inherit (import <nixpkgs/lib>)
flatten
remove
unique
;
inherit (import ./mylib.nix)
_0
_1
add_vec2d
eq
find_indices_in_arr2d
flatten_n
string_to_arr2d
sub_vec2d
tensor_product_with_self
vec2d_ll
;
find_antinodes = ps:
tensor_product_with_self ps
|> filter (p1p2: _0 p1p2 != _1 p1p2)
|> filter (p1p2: vec2d_ll (_0 p1p2) (_1 p1p2))
|> map (p1p2: let p1 = _0 p1p2; p2 = _1 p1p2; in [
(add_vec2d p1 (sub_vec2d p1 p2))
(add_vec2d p2 (sub_vec2d p2 p1))
])
;
is_in_map = s: p: 0 <= _1 p && _1 p < s && 0 <= _0 p && _0 p < s;
in
input:
let
m = input |> string_to_arr2d;
fs = m |> flatten |> unique |> remove ".";
pss = fs |> map (f: find_indices_in_arr2d (eq f) m);
in
pss
|> map find_antinodes
|> flatten_n 2
|> filter (is_in_map (length m))
|> unique
|> length