Skip to content

Commit

Permalink
solved day23 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
chinesedfan committed Jan 13, 2018
1 parent 39ede98 commit ba6ea06
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ For hardness: S(Simple), M(Middle), H(Hard).
| 20 | S/M | find stable/filter collisions |
| 21 | M/M | iterate grid |
| 22 | S/S | infinite grid |
| 23 | S/H | cpu/assembly to filter primes |
| 24 | S/S | dfs |
| 25 | S/- | turing machine |

Expand Down
61 changes: 60 additions & 1 deletion lib/2017/day23.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const rInstr = /(\w+) ([a-h]|-?\d+)(?: ([a-h]|-?\d+))?/;
let mul = 0;

module.exports = function(part, data) {
if (part == 1) {
f1(data);
} else {
f2();
}
};

function f1(data) {
const instrs = _.map(data.split('\n'), (line) => rInstr.exec(line));
let ip = 0;
let regs = {
Expand All @@ -21,7 +29,7 @@ module.exports = function(part, data) {
ip = exec(regs, instrs, ip);
}
console.log(mul);
};
}

function exec(regs, instrs, ip) {
const matches = instrs[ip];
Expand Down Expand Up @@ -51,3 +59,54 @@ function exec(regs, instrs, ip) {
function getVal(regs, r) {
return /[a-z]/.test(r) ? (regs[r] || 0) : parseInt(r);
}

// Count composite numbers between b and c (include c)
//
// set b 65
// set c b // c = 65
// jnz a 2
// jnz 1 5 // part 1, goto 9 directly
// mul b 100
// sub b -100000 // b = b * 100 + 100000
// set c b
// sub c -17000 // c = b + 17000
// set f 1
// set d 2
// set e 2
// set g d
// mul g e
// sub g b // g = d * e - b
// jnz g 2
// set f 0
// sub e -1 // e++
// set g e
// sub g b // e - b
// jnz g -8
// sub d -1 // d++
// set g d
// sub g b // d - b
// jnz g -13
// jnz f 2
// sub h -1 // h++
// set g b
// sub g c
// jnz g 2 // b - c
// jnz 1 3
// sub b -17 // b += 17
// jnz 1 -23 // goto 9
function f2() {
let b = 65;
b = b * 100 + 100000;
let c = b + 17000;

const candidates = _.map(_.range(b, c + 1, 17), (n) => ({n}));
for (let i = 2, max = Math.floor(Math.sqrt(c)); i <= max; i++) {
_.each(candidates, (item) => {
if (item.composite || (item.n % i)) return;
item.composite = true;
});
}

const count = _.filter(candidates, (item) => item.composite).length;
console.log(count);
}

0 comments on commit ba6ea06

Please sign in to comment.