47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import path from 'path';
|
|
import { readFile, sum } from "../../utils";
|
|
|
|
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
|
|
|
let grid: any[] = [];
|
|
|
|
let right = 0;
|
|
let up = 0;
|
|
|
|
let THistory: {x:number, y:number}[] = [];
|
|
|
|
let pos = {
|
|
|
|
H: { x: 0, y: 0 },
|
|
T: { x: 0, y: 0 }
|
|
}
|
|
|
|
input.split("\n").forEach(element => {
|
|
const [dir, num] = element.split(" ");
|
|
|
|
for (let n = 0; n < num; n++) {
|
|
const prevH = {x: pos.H.x, y: pos.H.y}
|
|
switch (dir) {
|
|
case "U":
|
|
pos.H = { x: pos.H.x, y: pos.H.y + 1 };
|
|
break;
|
|
case "D":
|
|
pos.H = { x: pos.H.x, y: pos.H.y - 1 };
|
|
break;
|
|
case "L":
|
|
pos.H = { x: pos.H.x - 1, y: pos.H.y };
|
|
break;
|
|
case "R":
|
|
pos.H = { x: pos.H.x + 1, y: pos.H.y };
|
|
break;
|
|
}
|
|
|
|
const distX = Math.abs(pos.H.x - pos.T.x);
|
|
const distY = Math.abs(pos.H.y - pos.T.y);
|
|
if(distX > 1 || distY > 1) pos.T = {x: prevH.x, y: prevH.y};
|
|
|
|
THistory.push(pos.T);
|
|
}
|
|
});
|
|
|
|
console.log("Part 1:", new Set(THistory.map(pos => pos.x + "-" + pos.y)).size); |