Skip to content

Commit 9b62e7c

Browse files
committed
solved bonus challenge
1 parent 61f6514 commit 9b62e7c

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ For hardness: S(Simple), M(Middle), H(Hard).
8181
| 23 | S/M | cpu/optimization |
8282
| 24 | M/M | bfs, permutation |
8383
| 25 | S/- | cpu, find first |
84+
85+
## Bonus
86+
87+
Download `input.txt` [here](https://gist.githubusercontent.com/topaz/15518587415ccd0468767aed4192bfd3/raw/c5bfd6a7d40eabe1ae8b9a0fb36a939cb0c5ddf4/bonuschallenge.txt).
88+
89+
- `YEAR=2016 node index.js 26 < input.txt > bonus.out.1`
90+
- `YEAR=2016 node index.js 8 2 < bonus.out.1`

Diff for: lib/2016/day26.js

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22
const _ = require('lodash');
33
const rInstr = /(\w+) ([a-d]|-?\d+)(?: ([a-d]|-?\d+))?/;
4-
const jnzCache = {};
4+
const output = [];
55

66
module.exports = function(part, data) {
77
const instrs = _.map(data.split('\n'), (line) => rInstr.exec(line));
88
let ip = 0;
99
let regs = {
10-
a: part == 1 ? 7 : 12,
10+
a: 0,
1111
b: 0,
1212
c: 0,
1313
d: 0
@@ -16,7 +16,6 @@ module.exports = function(part, data) {
1616
while (ip < instrs.length) {
1717
ip = exec(regs, instrs, ip);
1818
}
19-
console.log(ip + ':', regs.a, regs.b, regs.c, regs.d);
2019
};
2120

2221
function exec(regs, instrs, ip) {
@@ -34,30 +33,16 @@ function exec(regs, instrs, ip) {
3433
regs[matches[2]]--;
3534
break;
3635
case 'jnz':
37-
// try to find multiply
38-
if (/[a-d]/.test(matches[2])) {
39-
if (jnzCache[ip]) {
40-
const k = matches[2];
41-
const step = jnzCache[ip][k] / (jnzCache[ip][k] - regs[k]);
42-
_.each(regs, (val, key) => {
43-
regs[key] = jnzCache[ip][key] + (regs[key] - jnzCache[ip][key]) * step;
44-
});
45-
delete jnzCache[ip];
46-
} else {
47-
jnzCache[ip] = _.clone(regs);
48-
}
49-
}
50-
5136
if (getVal(regs, matches[2])) return ip + getVal(regs, matches[3]);
5237
break;
53-
case 'tgl':
54-
const target = instrs[ip + getVal(regs, matches[2])];
55-
if (!target) break;
56-
57-
if (typeof target[3] == 'undefined') {
58-
target[1] = target[1] == 'inc' ? 'dec' : 'inc';
38+
case 'out':
39+
const val = getVal(regs, matches[2]);
40+
const ch = String.fromCharCode(val);
41+
if (ch == '\n') {
42+
console.log(output.join(''));
43+
output.length = 0;
5944
} else {
60-
target[1] = target[1] == 'jnz' ? 'cpy' : 'jnz';
45+
output.push(ch);
6146
}
6247
break;
6348
default:

0 commit comments

Comments
 (0)