|
1 |
| -import { task } from '@alexaegis/advent-of-code-lib'; |
| 1 | +import { Direction, GridGraphNode, task } from '@alexaegis/advent-of-code-lib'; |
2 | 2 | import packageJson from '../package.json' assert { type: 'json' };
|
3 | 3 |
|
4 |
| -export const p2 = (_input: string): number => { |
5 |
| - return 0; |
| 4 | +const isArmCorrect = ( |
| 5 | + nodeA: GridGraphNode | undefined, |
| 6 | + nodeB: GridGraphNode | undefined, |
| 7 | +): boolean => { |
| 8 | + if (nodeA === undefined || nodeB === undefined) { |
| 9 | + return false; |
| 10 | + } |
| 11 | + |
| 12 | + return ( |
| 13 | + (nodeA.toString() === 'S' && nodeB.toString() === 'M') || |
| 14 | + (nodeA.toString() === 'M' && nodeB.toString() === 'S') |
| 15 | + ); |
| 16 | +}; |
| 17 | + |
| 18 | +export const p2 = (input: string): number => { |
| 19 | + let g = input.toGridGraph({ |
| 20 | + connectionDirections: Direction.allDirections, |
| 21 | + }); |
| 22 | + return g.nodeValues |
| 23 | + .filter((node) => node.toString() === 'A') |
| 24 | + .filter((node) => { |
| 25 | + let arm1Correct = isArmCorrect( |
| 26 | + node.getNeighbour(Direction.NORTHEAST), |
| 27 | + node.getNeighbour(Direction.SOUTHWEST), |
| 28 | + ); |
| 29 | + let arm2Correct = isArmCorrect( |
| 30 | + node.getNeighbour(Direction.NORTHWEST), |
| 31 | + node.getNeighbour(Direction.SOUTHEAST), |
| 32 | + ); |
| 33 | + |
| 34 | + return arm1Correct && arm2Correct; |
| 35 | + }).length; |
6 | 36 | };
|
7 | 37 |
|
8 |
| -await task(p2, packageJson.aoc); // 0 ~0.09ms |
| 38 | +await task(p2, packageJson.aoc); // 1900 ~79.72ms |
0 commit comments