-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.d
121 lines (102 loc) · 2.17 KB
/
day8.d
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module day8;
import std.stdio;
import std.file;
import std.algorithm : map;
import std.algorithm.comparison : equal;
import std.algorithm.iteration;
import std.array;
import std.range : enumerate;
import std.range : zip;
int[2] sub(int[2] a, int[2] b)
{
return [a[0] - b[0], a[1] - b[1]];
}
int[2] add(int[2] a, int[2] b)
{
return [a[0] + b[0], a[1] + b[1]];
}
int part1(dchar[][] content, int[2][][dchar] positions)
{
bool[50][50] has_antinode;
foreach (values; positions.byValue())
{
foreach (perm; values.permutations())
{
foreach (value1; zip(perm, perm[1 .. $]))
{
auto antinode = add(value1[0], sub(value1[0], value1[1]));
if (antinode[0] >= 0 && antinode[0] < 50 && antinode[1] >= 0 && antinode[1] < 50)
{
has_antinode[antinode[0]][antinode[1]] = true;
}
}
}
}
auto count = 0;
foreach (row; has_antinode)
{
foreach (col; row)
{
if (col)
count++;
}
}
return count;
}
int part2(dchar[][] content, int[2][][dchar] positions)
{
bool[50][50] has_antinode;
foreach (values; positions.byValue())
{
foreach (perm; values.permutations())
{
foreach (value1; zip(perm, perm[1 .. $]))
{
auto direction = sub(value1[0], value1[1]);
auto antinode = value1[0];
while (antinode[0] >= 0 && antinode[0] < 50 && antinode[1] >= 0 && antinode[1] < 50)
{
has_antinode[antinode[0]][antinode[1]] = true;
antinode = add(antinode, direction);
}
}
}
}
auto count = 0;
foreach (row; has_antinode)
{
foreach (col; row)
{
if (col)
count++;
}
}
return count;
}
void main()
{
auto content = File("input8.txt")
.byLine()
.map!(x => x.array())
.array();
int[2][][dchar] positions;
foreach (idx_y, line; content.enumerate(0))
{
foreach (idx_x, c; line.enumerate(0))
{
if (c == '.')
{
}
else if (c in positions)
{
positions[c] ~= [idx_x, idx_y];
}
else
{
positions[c] = [[idx_x, idx_y]];
}
}
}
writeln(part1(content, positions));
writeln(part2(content, positions));
}