diff --git a/resources b/resources index 65fbcae2..ccda2260 160000 --- a/resources +++ b/resources @@ -1 +1 @@ -Subproject commit 65fbcae27e6f023409683ca5703885698e34edf2 +Subproject commit ccda22607a78688e05f6842945b9be3fc9301b23 diff --git a/solutions/typescript/2024/06/src/p1.spec.ts b/solutions/typescript/2024/06/src/p1.spec.ts index 6208e31a..6e8544f7 100644 --- a/solutions/typescript/2024/06/src/p1.spec.ts +++ b/solutions/typescript/2024/06/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2024/06/src/p1.ts b/solutions/typescript/2024/06/src/p1.ts index 89e41ee9..79e48a9f 100644 --- a/solutions/typescript/2024/06/src/p1.ts +++ b/solutions/typescript/2024/06/src/p1.ts @@ -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(); + 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 diff --git a/solutions/typescript/2024/06/src/p2.ts b/solutions/typescript/2024/06/src/p2.ts index d2aa8106..259a7da6 100644 --- a/solutions/typescript/2024/06/src/p2.ts +++ b/solutions/typescript/2024/06/src/p2.ts @@ -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(); + const guardMovement = new Set(); + + 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