-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-13-1.js
66 lines (50 loc) · 1.52 KB
/
day-13-1.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
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 guests = {};
for (var i = 0; i < input.length - 1; i ++) {
var variables = input [i].split (" ");
if (typeof guests [variables [0]] == "undefined") {
guests [variables [0]] = {};
}
var other = variables [10].slice (0, -1);
guests [variables [0]] [other] = variables [2] == "gain" ? parseInt (variables [3]) : -1 * parseInt (variables [3]);
}
var everyone = [];
for (i in guests) {
everyone.push (i);
}
// console.log (everyone);
var possibilities = permutator (everyone);
// console.log (possibilities);
var happiest = 0;
for (var x = 0; x < possibilities.length; x++) {
var joy = 0;
// console.log (start);
for (var y = 0; y < 8; y ++) {
if (y == 7) {
joy += guests [possibilities [x] [y]] [possibilities [x] [0]];
joy += guests [possibilities [x] [0]] [possibilities [x] [y]];
} else {
joy += guests [possibilities [x] [y]] [possibilities [x] [y + 1]];
joy += guests [possibilities [x] [y + 1]] [possibilities [x] [y]];
}
}
if (joy > happiest) happiest = joy;
}
console.log (happiest);
alert (happiest); // 733