Skip to content

Commit

Permalink
Add solutions for day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 16, 2018
1 parent 0435eb6 commit 9bbe108
Show file tree
Hide file tree
Showing 5 changed files with 4,225 additions and 0 deletions.
22 changes: 22 additions & 0 deletions day-16/Sample.mjs
@@ -0,0 +1,22 @@
import operations from './operations';

const DEFINITION_REGEXP = /\d+/g;

class Sample {
constructor(definition) {
const numbers = definition.match(DEFINITION_REGEXP).map(Number);
this.before = numbers.slice(0, 4);
[this.opcode, this.inputA, this.inputB, this.outputC] = numbers.slice(4, 8);
this.after = numbers.slice(8);
}

probe() {
return operations.filter((operation) => {
const data = [].concat(this.before);
operation(data, this.inputA, this.inputB, this.outputC);
return data.toString() === this.after.toString();
});
}
}

export default Sample;
42 changes: 42 additions & 0 deletions day-16/index.mjs
@@ -0,0 +1,42 @@
#!/usr/bin/env node --experimental-modules --no-warnings

import Sample from './Sample';
import input from './input';
import operations from './operations';
import { day } from '../utils';

const [sampling, program] = input.split('\n\n\n\n');
const samples = sampling.split('\n\n').map(definition => new Sample(definition));

day(16).part(1).solution(() => (
samples.filter(sample => (
sample.probe().length >= 3
)).length
));

day(16).part(2).solution(() => {
const remaining = new Set(samples);
while (remaining.size) {
for (const sample of remaining) {
const candidates = sample.probe().filter(operation => (
operation.opcode === undefined
));
if (candidates.length === 1) {
const [operation] = candidates;
operation.opcode = sample.opcode;
}
if (candidates.length <= 1) {
remaining.delete(sample);
}
}
}

const data = [0, 0, 0, 0];
const lines = program.split('\n');
for (const line of lines) {
const [opcode, inputA, inputB, outputC] = line.match(/\d+/g).map(Number);
const operation = operations.find(candidate => candidate.opcode === opcode);
operation(data, inputA, inputB, outputC);
}
return data[0];
});
6 changes: 6 additions & 0 deletions day-16/input/example.mjs
@@ -0,0 +1,6 @@
export default `Before: [3, 2, 1, 1]
9 2 1 2
After: [3, 2, 2, 1]
9 1 1 1`;

0 comments on commit 9bbe108

Please sign in to comment.