2020 day 12 + visualisation

This commit is contained in:
tristan 2020-12-12 18:34:40 +00:00
parent 29b64b4715
commit 171802a184
8 changed files with 1038 additions and 3 deletions

38
2020/12/part1.js Normal file
View file

@ -0,0 +1,38 @@
// cool today's puzzle is basically a turtle
const fs = require('fs');
const instructions = fs.readFileSync('input.txt', 'utf-8').split('\n');
// position and direcion start at 0
let x = y = d = 0;
instructions.forEach((instruction, i) => {
console.log(x,y,d)
action = instruction[0];
arg = Number(instruction.slice(1));
switch (action) {
case 'N':
y+=arg;
break;
case 'S':
y-=arg;
break;
case 'E':
x+=arg;
break;
case 'W':
x-=arg;
break;
case 'L':
d-=arg;
break;
case 'R':
d+=arg;
break;
case 'F':
y -= Math.round(arg * Math.sin(d*Math.PI/180));
x += Math.round(arg * Math.cos(d*Math.PI/180));
break;
default:
console.log('instruction no', i+1, action,'invald')
}
})
console.log(x,y,d)
console.log(Math.abs(x)+ Math.abs(y))