Skip to content

Commit e7ffbd5

Browse files
committed
Working part one and part two
Didn't like this one: my code is pretty bad, and the instructions weren't clear at all.
1 parent cfb4d2f commit e7ffbd5

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

2016/10/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Answers
22

3-
| Part 1 | Part 2 |
4-
|----------|----------|
5-
| ` ` | ` ` |
3+
| Part 1 | Part 2 |
4+
|--------|---------|
5+
| `113` | `12803` |
66

77
## --- Day 10: Balance Bots ---
88

@@ -32,3 +32,7 @@ For example, consider the following instructions:
3232
In the end, output bin `0` contains a value-`5` microchip, output bin `1` contains a value-`2` microchip, and output bin `2` contains a value-`3` microchip. In this configuration, bot number _`2`_ is responsible for comparing value-`5` microchips with value-`2` microchips.
3333

3434
Based on your instructions, _what is the number of the bot_ that is responsible for comparing value-`61` microchips with value-`17` microchips?
35+
36+
## --- Part Two ---
37+
38+
What do you get if you _multiply together the values_ of one chip in each of outputs `0`, `1`, and `2`?

2016/10/bot-comparisons.js

+37-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BotComparisons {
2222

2323
parseInstructions(instructions_all = this.instructions) {
2424
// Create all bots and outputs, with bots INITialized with their chips
25-
const instructions = [];
25+
let instructions = [];
2626
instructions_all.forEach(instruction => {
2727
if (instruction.action === INIT) {
2828
const { bot } = instruction.goesTo;
@@ -66,38 +66,51 @@ class BotComparisons {
6666
Object.values(this.bots).forEach(chips => sortByValue(chips));
6767

6868
// Follow instructions
69-
instructions.forEach(instruction => {
70-
const { from, lowTo, highTo } = instruction;
71-
const bots_low = this.bots[from.bot].shift();
72-
const bots_high = this.bots[from.bot].pop();
69+
while (instructions.length) {
70+
const new_instructions = [];
71+
for (let instruction of instructions) {
72+
const { from, lowTo, highTo } = instruction;
73+
if (this.bots[from.bot].length < 2) {
74+
new_instructions.push(instruction);
75+
continue;
76+
}
7377

74-
if (bots_low == null || bots_high == null) {
75-
throw new Error(from.bot + ' compared ' + bots_low + ' and ' + bots_high);cc
76-
}
78+
const bots_low = this.bots[from.bot].shift();
79+
const bots_high = this.bots[from.bot].pop();
80+
81+
if (bots_low == null || bots_high == null) {
82+
throw new Error(from.bot + ' compared ' + bots_low + ' and ' + bots_high);cc
83+
}
7784

78-
this.botsCompared[from.bot].push(bots_low + ',' + bots_high);
85+
this.botsCompared[from.bot].push(bots_low + ',' + bots_high);
7986

80-
// This is too complicated / clever
81-
let collection_key = lowTo.bot == null ? 'outputs' : 'bots';
82-
const lowTo_key = collection_key === 'outputs' ? 'output' : 'bot';
83-
this[collection_key][lowTo[lowTo_key]].push(bots_low);
84-
sortByValue(this[collection_key][lowTo[lowTo_key]]);
87+
// This is too complicated / clever
88+
let collection_key = lowTo.bot == null ? 'outputs' : 'bots';
89+
const lowTo_key = collection_key === 'outputs' ? 'output' : 'bot';
90+
this[collection_key][lowTo[lowTo_key]].push(bots_low);
91+
sortByValue(this[collection_key][lowTo[lowTo_key]]);
8592

86-
collection_key = highTo.bot == null ? 'outputs' : 'bots';
87-
const highTo_key = collection_key === 'outputs' ? 'output' : 'bot';
88-
this[collection_key][highTo[highTo_key]].push(bots_high);
89-
sortByValue(this[collection_key][highTo[highTo_key]]);
90-
});
93+
collection_key = highTo.bot == null ? 'outputs' : 'bots';
94+
const highTo_key = collection_key === 'outputs' ? 'output' : 'bot';
95+
this[collection_key][highTo[highTo_key]].push(bots_high);
96+
sortByValue(this[collection_key][highTo[highTo_key]]);
97+
}
98+
99+
instructions = new_instructions;
100+
}
91101
}
92102

93103
whichBotCompared61And17() {
94-
const bots_who_compared_both = Object.keys(this.comparedBoth61And17);
95-
if (bots_who_compared_both.length === 1) {
96-
return bots_who_compared_both[0];
97-
} else {
98-
throw new Error(`More than one bot compared 61 and 17 chips: [${bots_who_compared_both.join(', ')}]`);
104+
for (let [bot, compared_arr] of Object.entries(this.botsCompared)) {
105+
if (compared_arr.includes('17,61')) {
106+
console.log(+bot);
107+
}
99108
}
100109
}
110+
111+
productOfOutputs(outputs = [0, 1, 2]) {
112+
return outputs.map(c => this.outputs[c][0]).reduce((a, b) => a * b, 1);
113+
}
101114
}
102115

103116
module.exports = BotComparisons;

2016/10/part-one.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ const { input, sampleInputs } = require('./input');
33
const BotComparisons = require('./bot-comparisons');
44

55
let bot_comparison = new BotComparisons(input);
6-
console.log(bot_comparison.whichBotCompared61And17());
6+
bot_comparison.whichBotCompared61And17();

2016/10/part-two.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
const assert = require('assert');
22
const { input, sampleInputs } = require('./input');
3+
const BotComparisons = require('./bot-comparisons');
4+
5+
let bot_comparison = new BotComparisons(input);
6+
console.log(bot_comparison.productOfOutputs());

0 commit comments

Comments
 (0)