-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-9-2.js
87 lines (68 loc) · 2.24 KB
/
day-9-2.js
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
// http://stackoverflow.com/questions/9960908/permutations-in-javascript
function permutator (inputArr) {
var results = [];
function permute (arr, memo) {
var cur, memo = memo || [];
for (var i = 0; i < arr.length; i++) {
cur = arr.splice (i, 1);
if (arr.length === 0) {
results.push (memo.concat (cur));
}
permute (arr.slice (), memo.concat (cur));
arr.splice (i, 0, cur [0]);
}
return results;
}
return permute (inputArr);
}
var input = document.getElementsByTagName ("pre") [0].textContent.split ("\n");
var destinations = [];
var routes = [];
var backwards = [];
var visited = [];
var largest = 0;
var total = 100000000000;
var parameters, start, stop, distance;
// Cache destinations, routes, and backward routes (backwards)
for (var i = 0; i < input.length - 1; i ++) {
parameters = input [i].split (" ");
start = parameters [0];
stop = parameters [2];
if (destinations.indexOf (start) == -1) destinations.push (start);
if (destinations.indexOf (stop) == -1) destinations.push (stop);
routes.push (start + "-" + stop);
backwards.push (stop + "-" + start);
}
var permutations = permutator (destinations);
// Iterate permutations against cache without repeating destinations
for (var i = 0; i < permutations.length; i ++) {
var permutation = permutations [i];
visited = [];
total = 0;
for (var x = 0; x < permutation.length - 1; x ++) {
start = permutation [x];
stop = permutation [x + 1];
route = routes.indexOf (start + "-" + stop);
backward = backwards.indexOf (start + "-" + stop);
if (route != -1) {
distance = parseInt (input [route].split (" ") [4]);
total += distance;
if (visited.indexOf (start) == -1) visited.push (start);
if (visited.indexOf (stop) == -1) visited.push (stop);
} else if (backward != -1) {
distance = parseInt (input [backward].split (" ") [4]);
total += distance;
if (visited.indexOf (start) == -1) visited.push (start);
if (visited.indexOf (stop) == -1) visited.push (stop);
} else {
break;
}
}
// Test and replace largest distance with visited total distance if all destinations have been visited
if (visited.length == destinations.length && total > largest) {
largest = total;
continue;
}
}
console.log (largest);
alert (largest); // 898