-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.d
150 lines (125 loc) · 3.45 KB
/
day12.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
module day12;
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.uni;
void main() {
string[] input = File("input/day12.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.array();
Cave[string] caves;
foreach (connection; input) {
string[] pts = connection.split("-");
string cid1 = pts[0];
string cid2 = pts[1];
if (!(cid1 in caves)) {
caves[cid1] = Cave(cid1);
}
if (!(cid2 in caves)) {
caves[cid2] = Cave(cid2);
}
caves[cid1].connections[cid2] = caves[cid2];
caves[cid2].connections[cid1] = caves[cid1];
}
part1(caves);
part2(caves);
}
void part1(Cave[string] caves) {
Path[] complete;
Path[] frontier;
frontier ~= [Path(["start"])];
while (frontier.length > 0) {
Path[] nextFrontier = [];
foreach (Path path; frontier) {
if (path.current() == "end") {
complete ~= [path];
continue;
}
foreach (Cave connection; caves[path.current()].connections) {
if (path.contains(connection.id) && connection.isSmall()) {
continue;
}
nextFrontier ~= [Path(path.path ~ [connection.id])];
}
}
frontier = nextFrontier;
}
writeln(complete.length);
}
void part2(Cave[string] caves) {
Path[] complete;
Path[] frontier;
frontier ~= [Path(["start"])];
while (frontier.length > 0) {
Path[] nextFrontier = [];
foreach (Path path; frontier) {
if (path.current() == "end") {
complete ~= [path];
continue;
}
foreach (Cave connection; caves[path.current()].connections) {
if (connection.id == "start") {
continue;
}
if (path.contains(connection.id) && connection.isSmall()) {
if (path.visitedSmallCaveTwice() == false) {
nextFrontier ~= [Path(path.path ~ [connection.id])];
} else {
continue;
}
}
nextFrontier ~= [Path(path.path ~ [connection.id])];
}
}
frontier = nextFrontier;
}
// looks like paths in complete array are not unique...
Path[string] completeDistinct;
foreach(c; complete) {
string key = c.to!string;
if (!(key in completeDistinct)) {
completeDistinct[key] = c;
}
}
writeln(completeDistinct.length);
}
struct Cave {
string id;
Cave[string] connections;
string toString() const {
return id;
}
bool isSmall() {
return isLower(id[0]);
}
}
struct Path {
string[] path;
string current() {
return path[$-1];
}
bool contains(string pid) {
return path.canFind(pid);
}
bool visitedSmallCaveTwice() {
for (int i = 0; i < path.length; i++) {
for (int j = 0; j < path.length; j++) {
if (i == j) continue;
if (path[i][0].isLower() && path[i] == path[j]) {
return true;
}
}
}
return false;
}
string toString() const {
return path.join(",");
}
}