Day 09
This commit is contained in:
parent
fc51293728
commit
d74ab6cf5a
3 changed files with 2118 additions and 1 deletions
|
@ -32,6 +32,7 @@
|
|||
"Day05": "cd ./src/05_SupplyStacks && deno run --allow-read=./input.txt SupplyStacks.ts",
|
||||
"Day06": "cd ./src/06_TuningTrouble && deno run --allow-read=./input.txt TuningTrouble.ts",
|
||||
"Day07": "cd ./src/07_NoSpaceLeftOnDevice && deno run --allow-read=./input.txt NoSpaceLeftOnDevice.ts",
|
||||
"Day08": "cd ./src/08_TreetopTreeHouse && deno run --allow-read=./input.txt TreetopTreeHouse.ts"
|
||||
"Day08": "cd ./src/08_TreetopTreeHouse && deno run --allow-read=./input.txt TreetopTreeHouse.ts",
|
||||
"Day09": "cd ./src/09_RopeBridge && deno run --allow-read=./input.txt RopeBridge.ts"
|
||||
}
|
||||
}
|
||||
|
|
116
src/09_RopeBridge/RopeBridge.ts
Normal file
116
src/09_RopeBridge/RopeBridge.ts
Normal file
|
@ -0,0 +1,116 @@
|
|||
const input = Deno.readTextFileSync('./input.txt').trimEnd().split('\n');
|
||||
|
||||
type Position = {
|
||||
x: number;
|
||||
y: number;
|
||||
};
|
||||
|
||||
type Instruction = {
|
||||
direction: 'U' | 'D' | 'L' | 'R';
|
||||
amount: number;
|
||||
};
|
||||
|
||||
function encode(position: Position): string {
|
||||
return `${position.x},${position.y}`;
|
||||
}
|
||||
|
||||
function makeRope(length = 2): Position[] {
|
||||
const rope: Position[] = [];
|
||||
for (let i = 0; i < Math.max(length, 1); i++) {
|
||||
rope.push({
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
}
|
||||
return rope;
|
||||
}
|
||||
|
||||
function moveRope(instructions: Instruction[], rope: Position[], visitedPositions: Set<string>) {
|
||||
for (const instruction of instructions) {
|
||||
for (let count = 0; count < instruction.amount; count++) {
|
||||
if (instruction.direction === 'U') {
|
||||
rope[0].y++;
|
||||
} else if (instruction.direction === 'D') {
|
||||
rope[0].y--;
|
||||
} else if (instruction.direction === 'L') {
|
||||
rope[0].x--;
|
||||
} else rope[0].x++;
|
||||
|
||||
for (let i = 1; i < rope.length; i++) {
|
||||
const head = rope[i - 1];
|
||||
const tail = rope[i];
|
||||
if (Math.abs(head.x - tail.x) > 1) {
|
||||
if (head.y !== tail.y) {
|
||||
(head.y > tail.y) ? tail.y++ : tail.y--;
|
||||
}
|
||||
if (head.x > tail.x) {
|
||||
tail.x++;
|
||||
} else tail.x--;
|
||||
} else if (Math.abs(head.y - tail.y) > 1) {
|
||||
if (head.x !== tail.x) {
|
||||
(head.x > tail.x) ? tail.x++ : tail.x--;
|
||||
}
|
||||
if (head.y > tail.y) {
|
||||
tail.y++;
|
||||
} else tail.y--;
|
||||
}
|
||||
}
|
||||
visitedPositions.add(encode(rope.at(-1)!));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function printMap(rope: Position[], padding = 5) {
|
||||
if (rope.length < 1) {
|
||||
console.log('Empty Rope');
|
||||
return;
|
||||
}
|
||||
|
||||
const coords: [number[], number[]] = [[], []];
|
||||
rope.forEach((knot) => {
|
||||
coords[0].push(knot.x);
|
||||
coords[1].push(knot.y);
|
||||
});
|
||||
const bounds = {
|
||||
minX: Math.min(...coords[0]) - padding,
|
||||
maxX: Math.max(...coords[0]) + padding + 1,
|
||||
minY: Math.min(...coords[1]) - padding,
|
||||
maxY: Math.max(...coords[1]) + padding + 1,
|
||||
};
|
||||
const width = bounds.maxX - bounds.minX;
|
||||
const height = bounds.maxY - bounds.minY;
|
||||
|
||||
const map: Array<string[]> = [];
|
||||
for (let row = 0; row < height; row++) {
|
||||
map.push([...''.padStart(width, '.')]);
|
||||
}
|
||||
|
||||
for (let index = rope.length - 1; index >= 0; index--) {
|
||||
const knot = rope[index]!;
|
||||
const posX = knot.x - bounds.minX;
|
||||
const posY = knot.y - bounds.minY;
|
||||
map[posY][posX] = (index % 10).toString();
|
||||
}
|
||||
map.reverse();
|
||||
map.forEach((row) => {
|
||||
console.log(row.join(''));
|
||||
});
|
||||
console.log();
|
||||
}
|
||||
|
||||
const instructions = input.map((line) => {
|
||||
const [left, right] = line.split(' ') as [string, string];
|
||||
return {
|
||||
direction: left,
|
||||
amount: parseInt(right),
|
||||
} as Instruction;
|
||||
});
|
||||
|
||||
// Part One
|
||||
const visitedPositions: Set<string> = new Set();
|
||||
moveRope(instructions, makeRope(), visitedPositions);
|
||||
console.log('Solution Part 1:', visitedPositions.size);
|
||||
|
||||
visitedPositions.clear();
|
||||
moveRope(instructions, makeRope(10), visitedPositions);
|
||||
console.log('Solution Part 2:', visitedPositions.size);
|
2000
src/09_RopeBridge/input.txt
Normal file
2000
src/09_RopeBridge/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue