-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.d
50 lines (43 loc) · 1.22 KB
/
part2.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
module day_06.part2;
import std.algorithm;
import std.array;
import std.container.rbtree;
import std.file;
import std.stdio;
void main() {
string input_file_name = "/home/brahle/dev/advent-of-code/2019/day_06/sample.2";
string[][string] edges;
int[string] distance;
auto input_file = File(input_file_name);
auto lines = input_file.byLine()
.map!(line => split(line, ")"));
foreach (line; lines) {
string parent = line[0].idup();
string child = line[1].idup();
// if (parent !in edges) {
// string[] t;
// edges[parent] = t;
// }
// if (child !in edges) {
// string[] t;
// edges[child] = t;
// }
distance[parent] = 0x3f3f3f3f;
distance[child] = 0x3f3f3f3f;
edges[parent] ~= child;
edges[child] ~= parent;
}
string[] Q;
distance["YOU"] = 0;
Q ~= "YOU";
for (int i = 0; i < Q.length; ++i) {
string current = Q[i];
foreach (next; edges[current]) {
if (distance[current] + 1 < distance[next]) {
distance[next] = distance[current] + 1;
Q ~= next;
}
}
}
writeln(distance["SAN"]);
}