9
This commit is contained in:
47
src/2022/day9/index.ts
Normal file
47
src/2022/day9/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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);
|
||||
Reference in New Issue
Block a user