-
Notifications
You must be signed in to change notification settings - Fork 17
/
bot-comparisons.js
116 lines (95 loc) · 3.12 KB
/
bot-comparisons.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
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
const { union } = require('lodash');
const { INIT, GIVE } = require('./input');
const sortByValue = arr => {
return arr.sort((a, b) => a - b);
};
class BotComparisons {
constructor(instructions_orig) {
this.instructions = JSON.parse(JSON.stringify(instructions_orig));
this.bots = {};
this.botsCompared = {};
this.outputs = {};
// Stupid way of figuring out part one
this.compared61 = {};
this.compared17 = {};
this.comparedBoth61And17 = {};
this.parseInstructions();
}
parseInstructions(instructions_all = this.instructions) {
// Create all bots and outputs, with bots INITialized with their chips
let instructions = [];
instructions_all.forEach(instruction => {
if (instruction.action === INIT) {
const { bot } = instruction.goesTo;
const { value } = instruction;
if (!this.bots[bot]) {
this.bots[bot] = [];
}
this.botsCompared[bot] = [];
this.bots[bot].push(value);
} else {
// Save GIVE actions to loop through later
instructions.push(instruction);
/*
action: GIVE,
from: { bot: 1 },
lowTo: { output: 1 },
highTo: { bot: 0 },
*/
for (const key of ['from', 'lowTo', 'highTo']) {
if (instruction[key].bot != null) {
if (!this.bots[instruction[key].bot]) {
this.bots[instruction[key].bot] = [];
}
this.botsCompared[instruction[key].bot] = [];
}
if (instruction[key].output != null) {
if (!this.outputs[instruction[key].output]) {
this.outputs[instruction[key].output] = [];
}
}
}
}
});
// Sorts bot's initial chips
Object.values(this.bots).forEach(chips => sortByValue(chips));
// Follow instructions
while (instructions.length) {
const new_instructions = [];
for (let instruction of instructions) {
const { from, lowTo, highTo } = instruction;
if (this.bots[from.bot].length < 2) {
new_instructions.push(instruction);
continue;
}
const bots_low = this.bots[from.bot].shift();
const bots_high = this.bots[from.bot].pop();
if (bots_low == null || bots_high == null) {
throw new Error(from.bot + ' compared ' + bots_low + ' and ' + bots_high);cc
}
this.botsCompared[from.bot].push(bots_low + ',' + bots_high);
// This is too complicated / clever
let collection_key = lowTo.bot == null ? 'outputs' : 'bots';
const lowTo_key = collection_key === 'outputs' ? 'output' : 'bot';
this[collection_key][lowTo[lowTo_key]].push(bots_low);
sortByValue(this[collection_key][lowTo[lowTo_key]]);
collection_key = highTo.bot == null ? 'outputs' : 'bots';
const highTo_key = collection_key === 'outputs' ? 'output' : 'bot';
this[collection_key][highTo[highTo_key]].push(bots_high);
sortByValue(this[collection_key][highTo[highTo_key]]);
}
instructions = new_instructions;
}
}
whichBotCompared61And17() {
for (let [bot, compared_arr] of Object.entries(this.botsCompared)) {
if (compared_arr.includes('17,61')) {
console.log(+bot);
}
}
}
productOfOutputs(outputs = [0, 1, 2]) {
return outputs.map(c => this.outputs[c][0]).reduce((a, b) => a * b, 1);
}
}
module.exports = BotComparisons;