diff --git a/README.md b/README.md index 1a45385..ed83566 100644 --- a/README.md +++ b/README.md @@ -81,3 +81,10 @@ For hardness: S(Simple), M(Middle), H(Hard). | 23 | S/M | cpu/optimization | | 24 | M/M | bfs, permutation | | 25 | S/- | cpu, find first | + +## Bonus + +Download `input.txt` [here](https://gist.githubusercontent.com/topaz/15518587415ccd0468767aed4192bfd3/raw/c5bfd6a7d40eabe1ae8b9a0fb36a939cb0c5ddf4/bonuschallenge.txt). + +- `YEAR=2016 node index.js 26 < input.txt > bonus.out.1` +- `YEAR=2016 node index.js 8 2 < bonus.out.1` diff --git a/lib/2016/day26.js b/lib/2016/day26.js index 6195eb1..5119438 100644 --- a/lib/2016/day26.js +++ b/lib/2016/day26.js @@ -1,13 +1,13 @@ 'use strict'; const _ = require('lodash'); const rInstr = /(\w+) ([a-d]|-?\d+)(?: ([a-d]|-?\d+))?/; -const jnzCache = {}; +const output = []; module.exports = function(part, data) { const instrs = _.map(data.split('\n'), (line) => rInstr.exec(line)); let ip = 0; let regs = { - a: part == 1 ? 7 : 12, + a: 0, b: 0, c: 0, d: 0 @@ -16,7 +16,6 @@ module.exports = function(part, data) { while (ip < instrs.length) { ip = exec(regs, instrs, ip); } - console.log(ip + ':', regs.a, regs.b, regs.c, regs.d); }; function exec(regs, instrs, ip) { @@ -34,30 +33,16 @@ function exec(regs, instrs, ip) { regs[matches[2]]--; break; case 'jnz': - // try to find multiply - if (/[a-d]/.test(matches[2])) { - if (jnzCache[ip]) { - const k = matches[2]; - const step = jnzCache[ip][k] / (jnzCache[ip][k] - regs[k]); - _.each(regs, (val, key) => { - regs[key] = jnzCache[ip][key] + (regs[key] - jnzCache[ip][key]) * step; - }); - delete jnzCache[ip]; - } else { - jnzCache[ip] = _.clone(regs); - } - } - if (getVal(regs, matches[2])) return ip + getVal(regs, matches[3]); break; - case 'tgl': - const target = instrs[ip + getVal(regs, matches[2])]; - if (!target) break; - - if (typeof target[3] == 'undefined') { - target[1] = target[1] == 'inc' ? 'dec' : 'inc'; + case 'out': + const val = getVal(regs, matches[2]); + const ch = String.fromCharCode(val); + if (ch == '\n') { + console.log(output.join('')); + output.length = 0; } else { - target[1] = target[1] == 'jnz' ? 'cpy' : 'jnz'; + output.push(ch); } break; default: