Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0435eb6
commit 9bbe108
Showing
5 changed files
with
4,225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default `Before: [3, 2, 1, 1] | ||
9 2 1 2 | ||
After: [3, 2, 2, 1] | ||
9 1 1 1`; |
Oops, something went wrong.