Published
Edited
Dec 20, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
md`The sum of the alignment parameters is **${part1}**`
Insert cell
Insert cell
program = puzzle.split(",")
Insert cell
view = intcomp(program).value
Insert cell
view.slice(-4)
Insert cell
dims = {
const y = view.filter( v => v == '\n'.charCodeAt(0) ).length - 1;
const x = (view.length - 1) / y; // extra newline at very end
return {x:x, y:y} // includes newline at end of line
}
Insert cell
intersections = {
const wall = '#'.charCodeAt(0);
return view.map((v, i) => {
if (v == wall) {
if ( view[i-1] == wall && view[i+1] == wall &&
view[i-dims.x] == wall && view[i+dims.x] == wall ) {
return [(i % dims.x), Math.floor(i / dims.x)];
}}
return 0
})
}
Insert cell
part1 = intersections.filter( v => v != 0 ).reduce((a,v) => a+v[0]*v[1], 0)
Insert cell
viewtxt = view.map( v => String.fromCharCode(v) ).join('')
Insert cell
Insert cell
md`The robot reports **${dustvalue}** dust.`
Insert cell
maxmem = 20;
Insert cell
Insert cell
Insert cell
segments = {
let walking=1000;
const inc = [-dims.x, 1, dims.x, -1];
const wall = '#'.charCodeAt(0);
let segs = [];
let current, counter = 0, turn = 'R';
let start = view.indexOf('^'.charCodeAt(0));
let dir = 0; // 0 to 3 is keep turning right: N,E,S,W
while (walking--) {
//go straight
start += inc[dir];
if (view[start] == wall) { // good
counter++; continue;
}
segs.push({turn:turn, steps:counter});
//off rail
start -= inc[dir];
counter = 0;
//check where to turn
if (view[start+inc[(dir+1)%4]] == wall) {// turn right
turn = 'R';
dir = (dir+1)%4;
continue;
}
if (view[start+inc[(dir+3)%4]] == wall) {// turn left
turn = 'L';
dir = (dir+3)%4;
continue;
}
break;
}
return segs.slice(1);
}
Insert cell
Insert cell
path = {
// find move combo recursively: solution is move + solution of shorter path and one less move option
// moves: array of moves (<= 3)
// move: array of segments (<= 10)
let path3F = function(segs, moves, prog){
if (segs.length == 0) { return [prog, moves, segs] } // found a solution
let m = moves.findIndex(m => m.join() == segs.slice(0, m.length).join());
if (m !== -1) {
return path3F(segs.slice(moves[m].length), moves, prog.concat(m)); } //could use existing move, add to prog
let slot = [0,1,2].find(m => !prog.includes(m));
if (slot == undefined) { return false }
let window = Math.floor(segs.length / 2);
while (window-- > 1) { // try all moves
m = segs.slice(0, window);
if (m.join(',').length > maxmem) { continue; } // too long
moves[slot] = m;
let path = path3F(segs.slice(window), moves, prog.concat(slot)); // try shorter path
if (path) {return path} // should not happen
}
return false;//[2, segs, moves, prog] // tried all here, try next in upper slot
}
//debugger;
return path3F(segments.map(s => s.turn+","+s.steps), [], [])
}
Insert cell
dust = {
function tocharCode(s) {return s.map(v => v.charCodeAt(0))}
const cmds = ['A','B','C'];//.map(v => v.charCodeAt(0));
let main = (path[0].map(v => cmds[v]).join(',')+'\n').split('');
let moves = path[1].map(m => (m.join(',')+'\n').split(''));
let feed = ("n"+'\n').split('');
let input = main.concat(...moves,feed);
let prog = program;
prog[0] = 2;
//return tocharCode(input).reverse()
return intcomp(prog, tocharCode(input).reverse())
}
Insert cell
Insert cell
dustvalue = dust.value.slice(-1)[0]
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more