-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.nix
101 lines (90 loc) · 2.01 KB
/
default.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{ nixpkgs, lib, ... }:
with nixpkgs.lib;
with lib;
let
input = fileContents ./input;
parseInput =
input:
let
grid = map stringToCharacters (splitString "\n" input);
byChars = groupBy (el: el.c) (flatten (arr2.imap (x: y: c: { inherit x y c; }) grid));
in
{
width = arr2.width grid;
height = arr2.height grid;
locs = removeAttrs byChars [ "." ];
};
antiNodes = l: r: [
{
x = l.x - (r.x - l.x);
y = l.y - (r.y - l.y);
}
{
x = r.x + (r.x - l.x);
y = r.y + (r.y - l.y);
}
];
findAntinodes =
charCoords:
flatten (
mapCartesianProduct ({ l, r }: if l == r then [ ] else antiNodes l r) {
l = charCoords;
r = charCoords;
}
);
valid =
an: w: h:
an.x >= 0 && an.y >= 0 && an.x < w && an.y < h;
part1Answer =
input:
let
p = parseInput input;
ans = flatten (map findAntinodes (attrValues p.locs));
in
length (unique (filter (an: valid an p.width p.height) ans));
antiNodesL =
l: r: w: h:
let
nl = {
x = l.x - (r.x - l.x);
y = l.y - (r.y - l.y);
};
in
if valid nl w h then [ nl ] ++ (antiNodesL nl l w h) else [ ];
antiNodesR =
l: r: w: h:
let
nr = {
x = r.x + (r.x - l.x);
y = r.y + (r.y - l.y);
};
in
if valid nr w h then [ nr ] ++ (antiNodesR r nr w h) else [ ];
antiNodes2 =
l: r: w: h:
[
{ inherit (l) x y; }
{ inherit (r) x y; }
]
++ (antiNodesL r l w h)
++ (antiNodesR l r w h);
findAntinodes2 =
width: height: charCoords:
flatten (
mapCartesianProduct ({ l, r }: if l == r then [ ] else antiNodes2 l r width height) {
l = charCoords;
r = charCoords;
}
);
part2Answer =
input:
let
p = parseInput input;
ans = flatten (map (v: findAntinodes2 p.width p.height v) (attrValues p.locs));
in
length (unique ans);
in
{
part1 = part1Answer input;
part2 = part2Answer input;
}