This commit is contained in:
Aelita4 2022-12-09 15:05:49 +01:00
parent 69b0f79024
commit 22b16768f3
Signed by: Aelita4
GPG Key ID: E44490C2025906C1
2 changed files with 2052 additions and 0 deletions

52
day09/index.js Normal file
View File

@ -0,0 +1,52 @@
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 head = { x: 0, y: 0}, tail = { x: 0, y: 0 };
const visited = new Map([[ `0:0`, true ]]);
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++) {
const oldHead = {};
oldHead.x = head.x;
oldHead.y = head.y;
head.x += x;
head.y += y;
if(!isTailAdjacentToHead(head, tail)) {
tail.x = oldHead.x;
tail.y = oldHead.y;
visited.set(`${tail.x}:${tail.y}`, true);
}
}
});
console.log(visited.size);

2000
day09/movements.txt Normal file

File diff suppressed because it is too large Load Diff