-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.dart
88 lines (74 loc) · 2.21 KB
/
day12.dart
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
import './utils.dart';
List<List<Node>> findPaths(Node start, List<Node> currPath, Node bonusCave) {
List<List<Node>> paths = [];
// here we define rules for available connections
List<Node> availableConns = start.connections.where((n) {
bool bonus =
n == bonusCave && currPath.where((x) => x == n).toList().length < 2;
return !currPath.contains(n) || n.name == n.name.toUpperCase() || bonus;
}).toList();
List<Node> newCurrPath = List.from(currPath);
newCurrPath.add(start);
// ending condition for recursion
if (availableConns.length == 0 || start.name == 'end') {
return [newCurrPath];
}
// get paths
for (Node adj in availableConns) {
List<List<Node>> adjPaths = findPaths(adj, newCurrPath, bonusCave);
for (List<Node> adjPath in adjPaths) {
paths.add(adjPath);
}
}
return paths;
}
class Node {
String name;
Set<Node> connections = Set();
Node(this.name);
connect(Node n) {
this.connections.add(n);
}
@override
String toString() {
return this.name;
}
}
int solve(List<String> lines) {
// parse lines into Nodes
Map<String, Node> nodesMap = {};
for (String line in lines) {
var parts = line.split('-');
String first = parts[0];
String second = parts[1];
Node firstN = nodesMap[first] ?? Node(first);
Node secondN = nodesMap[second] ?? Node(second);
firstN.connect(secondN);
secondN.connect(firstN);
nodesMap[first] = firstN;
nodesMap[second] = secondN;
}
List<Node> nodes = nodesMap.values.toList();
List<Node> bonusCaves = nodes
.where((n) =>
n.name == n.name.toLowerCase() &&
n.name != 'end' &&
n.name != 'start')
.toList();
Set<String> paths = Set();
Node startingNode = nodes.firstWhere((n) => n.name == 'start');
// consider each small cave as a separate case, gather results in Set to
// avoid duplicates
for (Node bonusCave in bonusCaves) {
paths.addAll(findPaths(startingNode, [], bonusCave)
.where((p) => p.last.name == 'end')
.map((p) => p.join(','))
.toList());
}
return paths.length;
}
void main() async {
List<String> lines = await readlines('day12test.txt');
int result = solve(lines);
print(result);
}