Skip to content

Commit

Permalink
feat: solved 2024 day 6 p1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 8, 2024
1 parent 630d133 commit 8c483ad
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
2 changes: 1 addition & 1 deletion resources
4 changes: 2 additions & 2 deletions solutions/typescript/2024/06/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2024 06 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(4602);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(41);
});
});
});
34 changes: 30 additions & 4 deletions solutions/typescript/2024/06/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

export const p1 = (_input: string): number => {
return 0;
export const p1 = (input: string): number => {
const g = input.toGridGraph({});
const guardNode = g.findNode((node) => node.value === '^');
if (!guardNode) {
throw new Error('Guard not found');
}
guardNode.setValue('.');
let guardPosition = guardNode.coordinate;
let guardDirection = Direction.NORTH.clone();
console.log(guardPosition);
const guardPath = new Set<string>();
guardPath.add(guardPosition.toString());

while (true) {
let forwardPosition = guardPosition.add(guardDirection);
let forwardNode = g.getNode(forwardPosition);
if (!forwardNode) {
break;
}
if (forwardNode.value === '#') {
guardDirection = guardDirection.rotateLeft();
}

guardPosition.addMut(guardDirection);
guardPath.add(guardPosition.toString());
}
//g.print((n) => (guardPath.has(n.coordinate.toString()) ? 'X' : n.toString()));
return guardPath.size;
};

await task(p1, packageJson.aoc); // 0 ~0.09ms
await task(p1, packageJson.aoc); // 4602 ~0.09ms
64 changes: 60 additions & 4 deletions solutions/typescript/2024/06/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json' assert { type: 'json' };

export const p2 = (_input: string): number => {
return 0;
export const p2 = (input: string): number => {
const g = input.toGridGraph({});
const possibleObstructionCoordinates = g.nodeValues
.filter((node) => node.value === '.')
.map((node) => node.coordinate);
return possibleObstructionCoordinates.filter((possibleObstructionCoordinate) => {
const g = input.toGridGraph({});

const guardNode = g.findNode((node) => node.value === '^');
if (!guardNode) {
throw new Error('Guard not found');
}

const obsructionNode = g.getNode(possibleObstructionCoordinate);
if (!obsructionNode) {
throw new Error('Obstruction not found');
}

obsructionNode.setValue('#');
guardNode.setValue('.');
let guardPosition = guardNode.coordinate;
let guardDirection = Direction.NORTH.clone();
const guardPath = new Set<string>();
const guardMovement = new Set<string>();

guardPath.add(guardPosition.toString());
guardMovement.add(guardPosition.toString() + '+' + guardDirection.toString());

let isLoop = false;
let i = 0;
while (true) {
i++;
let forwardPosition = guardPosition.add(guardDirection);
let forwardNode = g.getNode(forwardPosition);
if (!forwardNode) {
// Not a loop
break;
}
if (forwardNode.value === '#') {
guardDirection = guardDirection.rotateLeft();
}

const pathLengthBeforeStep = guardMovement.size;
guardPosition.addMut(guardDirection);
guardPath.add(guardPosition.toString());
guardMovement.add(guardPosition.toString() + '+' + guardDirection.toString());

const pathLengthAfterStep = guardMovement.size;
if (pathLengthBeforeStep === pathLengthAfterStep) {
isLoop = true;
break;
}
}
//g.print((n) => (guardPath.has(n.coordinate.toString()) ? 'X' : n.toString()));
//console.log('--------', daysWithoutUpdate, isLoop, i);
// possibleObstructionNode.setValue('.');
return isLoop;
}).length;
};

await task(p2, packageJson.aoc); // 0 ~0.09ms
await task(p2, packageJson.aoc); // 4602 ~0.09ms

0 comments on commit 8c483ad

Please sign in to comment.