-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
Submodule resources
updated
from 65fbca to ccda22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |