Compare commits
No commits in common. "7d835cdadf7d9f71f4f2ff88c51b58e7a24488c0" and "69b0f79024f63591c00d49719aae82fdd538f0f9" have entirely different histories.
7d835cdadf
...
69b0f79024
|
@ -1,67 +0,0 @@
|
||||||
import { readFileSync } from "fs";
|
|
||||||
|
|
||||||
const input = readFileSync("movements.txt", "utf-8");
|
|
||||||
|
|
||||||
function isTailAdjacentToHead(head, tail) {
|
|
||||||
if(Math.abs(tail.x - head.x) > 1) return false;
|
|
||||||
if(Math.abs(tail.y - head.y) > 1) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const initX = 11, initY = 5;
|
|
||||||
const rope = [
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY },
|
|
||||||
{ x: initX, y: initY }
|
|
||||||
];
|
|
||||||
|
|
||||||
const visited = new Map();
|
|
||||||
|
|
||||||
input.split('\n').forEach(line => {
|
|
||||||
const operations = line.split(' ');
|
|
||||||
|
|
||||||
let x = 0, y = 0;
|
|
||||||
|
|
||||||
switch(operations[0]) {
|
|
||||||
case "U":
|
|
||||||
y++;
|
|
||||||
break;
|
|
||||||
case "D":
|
|
||||||
y--;
|
|
||||||
break;
|
|
||||||
case "L":
|
|
||||||
x--;
|
|
||||||
break;
|
|
||||||
case "R":
|
|
||||||
x++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(let i = 0; i < parseInt(operations[1]); i++) {
|
|
||||||
rope[0].x += x;
|
|
||||||
rope[0].y += y;
|
|
||||||
|
|
||||||
for(let ropeIndex = 1; ropeIndex < rope.length; ropeIndex++) {
|
|
||||||
if(!isTailAdjacentToHead(rope[ropeIndex - 1], rope[ropeIndex])) {
|
|
||||||
if(rope[ropeIndex - 1].x !== rope[ropeIndex].x && rope[ropeIndex - 1].y !== rope[ropeIndex].y) {
|
|
||||||
rope[ropeIndex].x += (rope[ropeIndex - 1].x - rope[ropeIndex].x) > 0 ? 1 : -1;
|
|
||||||
rope[ropeIndex].y += (rope[ropeIndex - 1].y - rope[ropeIndex].y) > 0 ? 1 : -1;
|
|
||||||
} else if(rope[ropeIndex - 1].x === rope[ropeIndex].x) {
|
|
||||||
rope[ropeIndex].y += (rope[ropeIndex - 1].y - rope[ropeIndex].y) > 0 ? 1 : -1;
|
|
||||||
} else if(rope[ropeIndex - 1].y === rope[ropeIndex].y) {
|
|
||||||
rope[ropeIndex].x += (rope[ropeIndex - 1].x - rope[ropeIndex].x) > 0 ? 1 : -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
visited.set(`${rope[rope.length - 1].x}:${rope[rope.length - 1].y}`, true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(visited.size);
|
|
2000
day09/movements.txt
2000
day09/movements.txt
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue