Skip to content

Commit

Permalink
2019 Day 19 Puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleGough committed Nov 1, 2024
1 parent d0b9013 commit 6d8b7b0
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 12 deletions.
9 changes: 9 additions & 0 deletions solutions/2019/19/day19.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { intcodeComputer } from '../05/day05.helper';

export const isTractorBeam = (
nums: number[],
x: number,
y: number
): boolean => {
return intcodeComputer([...nums], [x, y]) === 1;
};
15 changes: 15 additions & 0 deletions solutions/2019/19/day19.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { day19p1 } from './p1';
import { day19p2 } from './p2';
import { getPuzzle } from '@utilities/getPuzzle';

const { input } = getPuzzle(__dirname);

describe('Day 19 Puzzle', () => {
test('Part 1 Input', () => {
expect(day19p1(input)).toBe(192);
});

test('Part 2 Input', () => {
expect(day19p2(input)).toBe(8_381_082);
});
});
1 change: 1 addition & 0 deletions solutions/2019/19/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
109,424,203,1,21101,11,0,0,1106,0,282,21102,18,1,0,1105,1,259,1201,1,0,221,203,1,21101,0,31,0,1105,1,282,21102,1,38,0,1106,0,259,21001,23,0,2,22101,0,1,3,21102,1,1,1,21101,57,0,0,1106,0,303,1202,1,1,222,21002,221,1,3,20102,1,221,2,21101,259,0,1,21102,1,80,0,1105,1,225,21101,83,0,2,21101,91,0,0,1105,1,303,2102,1,1,223,20102,1,222,4,21102,259,1,3,21101,225,0,2,21101,225,0,1,21102,1,118,0,1105,1,225,20102,1,222,3,21101,0,51,2,21102,1,133,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,1,148,0,1106,0,259,1201,1,0,223,21002,221,1,4,21002,222,1,3,21101,13,0,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,108,20207,1,223,2,21002,23,1,1,21102,-1,1,3,21101,0,214,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2102,1,-4,249,21202,-3,1,1,21202,-2,1,2,22102,1,-1,3,21101,0,250,0,1105,1,225,22102,1,1,-4,109,-5,2106,0,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,22101,0,-2,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22101,0,-2,3,21101,0,343,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2105,1,0
21 changes: 21 additions & 0 deletions solutions/2019/19/p1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getPuzzle } from '@utilities/getPuzzle';
import { run } from '@utilities/run';
import { isTractorBeam } from './day19.helper';

export const day19p1 = (input: string) => {
const nums = input.split(',').map(Number);
let count = 0;

for (let y = 0; y < 50; y++) {
for (let x = 0; x < 50; x++) {
if (isTractorBeam(nums, x, y)) {
count += 1;
}
}
}

return count;
};

const input = getPuzzle(__dirname).input;
run(() => day19p1(input)); // 192
49 changes: 49 additions & 0 deletions solutions/2019/19/p2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { getPuzzle } from '@utilities/getPuzzle';
import { run } from '@utilities/run';
import { isTractorBeam } from './day19.helper';

export const day19p2 = (input: string) => {
const nums = input.split(',').map(Number);
let found = false;
let x = 50;
let y = 50;
const size = 100;

while (!found) {
x += 1;
y = findLowestPoint(nums, x, y);

if (y < size) continue;

if (canShipFit(nums, x, y, size)) {
found = true;
}
}

return x * 10000 + (y - size + 1);
};

const findLowestPoint = (nums: number[], x: number, y: number): number => {
while (!isTractorBeam(nums, x, y) || isTractorBeam(nums, x, y + 1)) {
y++;
}

return y;
};

const canShipFit = (
nums: number[],
x: number,
y: number,
size: number
): boolean => {
return (
isTractorBeam(nums, x + size - 1, y - size + 1) &&
isTractorBeam(nums, x, y - size + 1) &&
isTractorBeam(nums, x + size - 1, y) &&
isTractorBeam(nums, x, y)
);
};

const input = getPuzzle(__dirname).input;
run(() => day19p2(input)); // 8381082
2 changes: 1 addition & 1 deletion solutions/2021/01/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export const day01p1 = (input: string) => {
return increaseCount;
};

const input = getPuzzle(__dirname).example;
const input = getPuzzle(__dirname).input;
run(() => day01p1(input)); // 1709
2 changes: 1 addition & 1 deletion solutions/2021/12/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ interface QueueItem {
}

const input = getPuzzle(__dirname).input;
run(() => day12p1(input)); // TODO
run(() => day12p1(input)); // 3421
2 changes: 1 addition & 1 deletion solutions/2021/13/p1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ export const day13p1 = (input: string) => {
};

const input = getPuzzle(__dirname).input;
run(() => day13p1(input)); // TODO
run(() => day13p1(input)); // 814
15 changes: 6 additions & 9 deletions solutions/2021/22/p2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ class Cube {

intersects(other: Cube): boolean {
return !(
// TODO check if >= or > for all cases
(
this.minX >= other.maxX ||
this.maxX <= other.minX ||
this.minY >= other.maxY ||
this.maxY <= other.minY ||
this.minZ >= other.maxZ ||
this.maxZ <= other.minZ
)
this.minX >= other.maxX ||
this.maxX <= other.minX ||
this.minY >= other.maxY ||
this.maxY <= other.minY ||
this.minZ >= other.maxZ ||
this.maxZ <= other.minZ
);
}

Expand Down

0 comments on commit 6d8b7b0

Please sign in to comment.