|
1 |
| -import { Direction, task } from '@alexaegis/advent-of-code-lib'; |
| 1 | +import { BoundingBox, Direction, task, Vec2, type Vec2String } 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 |
| - const g = input.toGridGraph({}); |
6 |
| - const possibleObstructionCoordinates = g.nodeValues |
7 |
| - .filter((node) => node.value === '.') |
8 |
| - .map((node) => node.coordinate); |
9 |
| - return possibleObstructionCoordinates.filter((possibleObstructionCoordinate) => { |
10 |
| - const g = input.toGridGraph({}); |
11 |
| - |
12 |
| - const guardNode = g.findNode((node) => node.value === '^'); |
13 |
| - if (!guardNode) { |
14 |
| - throw new Error('Guard not found'); |
| 4 | +export interface PatrolResult { |
| 5 | + path: Set<Vec2String>; |
| 6 | + encounters: Set<Vec2String>; |
| 7 | + isLoop: boolean; |
| 8 | + movement: Move[]; |
| 9 | +} |
| 10 | + |
| 11 | +interface Move { |
| 12 | + position: Vec2; |
| 13 | + direction: Direction; |
| 14 | +} |
| 15 | + |
| 16 | +export const patrol = (obscructions: Set<string>, map: BoundingBox, from: Vec2): PatrolResult => { |
| 17 | + let guardPosition = from.clone(); |
| 18 | + let guardDirection = Direction.NORTH.clone(); |
| 19 | + const path = new Set<Vec2String>(); |
| 20 | + const encounters = new Set<Vec2String>(); |
| 21 | + const guardMovement = new Set<string>(); |
| 22 | + const movementPath: Move[] = []; |
| 23 | + |
| 24 | + path.add(guardPosition.toString()); |
| 25 | + guardMovement.add(guardPosition.toString() + '+' + guardDirection.toString()); |
| 26 | + movementPath.push({ position: guardPosition, direction: guardDirection }); |
| 27 | + |
| 28 | + let isLoop = false; |
| 29 | + |
| 30 | + while (true) { |
| 31 | + let forwardPosition = guardPosition.add(guardDirection); |
| 32 | + if (!forwardPosition.isWithin(map)) { |
| 33 | + break; |
15 | 34 | }
|
16 | 35 |
|
17 |
| - const obsructionNode = g.getNode(possibleObstructionCoordinate); |
18 |
| - if (!obsructionNode) { |
19 |
| - throw new Error('Obstruction not found'); |
| 36 | + if (obscructions.has(forwardPosition.toString())) { |
| 37 | + encounters.add(forwardPosition.toString()); |
| 38 | + guardDirection = guardDirection.rotateLeft(); |
20 | 39 | }
|
21 | 40 |
|
22 |
| - obsructionNode.setValue('#'); |
23 |
| - guardNode.setValue('.'); |
24 |
| - let guardPosition = guardNode.coordinate; |
25 |
| - let guardDirection = Direction.NORTH.clone(); |
26 |
| - const guardPath = new Set<string>(); |
27 |
| - const guardMovement = new Set<string>(); |
28 |
| - |
29 |
| - guardPath.add(guardPosition.toString()); |
30 |
| - guardMovement.add(guardPosition.toString() + '+' + guardDirection.toString()); |
31 |
| - |
32 |
| - let isLoop = false; |
33 |
| - let i = 0; |
34 |
| - while (true) { |
35 |
| - i++; |
36 |
| - let forwardPosition = guardPosition.add(guardDirection); |
37 |
| - let forwardNode = g.getNode(forwardPosition); |
38 |
| - if (!forwardNode) { |
39 |
| - // Not a loop |
40 |
| - break; |
41 |
| - } |
42 |
| - if (forwardNode.value === '#') { |
43 |
| - guardDirection = guardDirection.rotateLeft(); |
44 |
| - } |
45 |
| - |
46 |
| - const pathLengthBeforeStep = guardMovement.size; |
47 |
| - guardPosition.addMut(guardDirection); |
48 |
| - guardPath.add(guardPosition.toString()); |
49 |
| - guardMovement.add(guardPosition.toString() + '+' + guardDirection.toString()); |
50 |
| - |
51 |
| - const pathLengthAfterStep = guardMovement.size; |
52 |
| - if (pathLengthBeforeStep === pathLengthAfterStep) { |
53 |
| - isLoop = true; |
54 |
| - break; |
55 |
| - } |
| 41 | + guardPosition.addMut(guardDirection); |
| 42 | + path.add(guardPosition.toString()); |
| 43 | + const move = guardPosition.toString() + '+' + guardDirection.toString(); |
| 44 | + movementPath.push({ position: guardPosition, direction: guardDirection }); |
| 45 | + |
| 46 | + if (guardMovement.has(move)) { |
| 47 | + isLoop = true; |
| 48 | + break; |
| 49 | + } else { |
| 50 | + guardMovement.add(move); |
56 | 51 | }
|
57 |
| - //g.print((n) => (guardPath.has(n.coordinate.toString()) ? 'X' : n.toString())); |
58 |
| - //console.log('--------', daysWithoutUpdate, isLoop, i); |
59 |
| - // possibleObstructionNode.setValue('.'); |
60 |
| - return isLoop; |
| 52 | + } |
| 53 | + return { |
| 54 | + path, |
| 55 | + encounters, |
| 56 | + isLoop, |
| 57 | + }; |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Since the guard can only rotate right, and 90 degrees, a loop always |
| 62 | + * will look like a rectangle |
| 63 | + */ |
| 64 | +export const matchEnclosure = ( |
| 65 | + position: Vec2, |
| 66 | + direction: Direction, |
| 67 | + pivotPlacement: Direction, |
| 68 | + boundingBox: BoundingBox, |
| 69 | + obscructions: Set<Vec2String>, |
| 70 | +): boolean => { |
| 71 | + const possibleObstruction = position.add(direction); |
| 72 | + if (obscructions.has(possibleObstruction.toString())) { |
| 73 | + return false; // Already obsctructed |
| 74 | + } |
| 75 | + |
| 76 | + const firstCorner = position.add(direction.rotateLeft(1), { |
| 77 | + times: (v) => !obscructions.has(v.toString()) && boundingBox.contains(v), |
| 78 | + }); |
| 79 | + console.log('fc', firstCorner); |
| 80 | + |
| 81 | + return false; |
| 82 | +}; |
| 83 | + |
| 84 | +export const createParametricEnclosure = |
| 85 | + (pivot: Vec2, pivotPlacement: Direction) => (size: Vec2) => {}; |
| 86 | + |
| 87 | +export const createEnclosure = (pivot: Vec2, pivotPlacement: Direction, size: Vec2): Vec2[] => { |
| 88 | + const corners = [pivot]; |
| 89 | + |
| 90 | + const vertical = pivotPlacement.y > 0 ? new Vec2(1, -size.y) : new Vec2(-1, size.y); |
| 91 | + const horizontal = pivotPlacement.x > 0 ? new Vec2(-size.x, -1) : new Vec2(size.x, 1); |
| 92 | + // Horizontal |
| 93 | + |
| 94 | + corners.push(pivot.add(vertical)); |
| 95 | + corners.push(pivot.add(horizontal)); |
| 96 | + |
| 97 | + corners.push(pivot.add(vertical.add(horizontal))); |
| 98 | + |
| 99 | + return corners; |
| 100 | +}; |
| 101 | + |
| 102 | +const enclosurePivotMap = { |
| 103 | + [Direction.NORTH.toString()]: Direction.NORTHWEST, |
| 104 | + [Direction.EAST.toString()]: Direction.NORTHEAST, |
| 105 | + [Direction.SOUTH.toString()]: Direction.SOUTHEAST, |
| 106 | + [Direction.WEST.toString()]: Direction.SOUTHWEST, |
| 107 | +} as const; |
| 108 | + |
| 109 | +export const p2 = (input: string): number => { |
| 110 | + const g = input.toGridGraph(); |
| 111 | + const boundingBox = g.boundingBox(); |
| 112 | + |
| 113 | + const obstructions = new Set( |
| 114 | + g.nodeValues.filter((node) => node.value === '#').map((node) => node.coordinate.toString()), |
| 115 | + ); |
| 116 | + |
| 117 | + const freeNodes = new Set( |
| 118 | + g.nodeValues.filter((node) => node.value === '.').map((node) => node.coordinate.toString()), |
| 119 | + ); |
| 120 | + |
| 121 | + const guardNode = g.findNode((node) => node.value === '^'); |
| 122 | + if (!guardNode) { |
| 123 | + throw new Error('Guard not found'); |
| 124 | + } |
| 125 | + |
| 126 | + g.print(); |
| 127 | + console.log('23332323----------------------'); |
| 128 | + |
| 129 | + const originalPatrolResult = patrol(obstructions, boundingBox, guardNode.coordinate); |
| 130 | + |
| 131 | + const enclosure = createEnclosure(new Vec2(3, 3), Direction.SOUTHWEST, new Vec2(3, 3)); |
| 132 | + |
| 133 | + g.print((n) => |
| 134 | + enclosure.map((e) => e.toString()).includes(n.coordinate.toString()) |
| 135 | + ? enclosure |
| 136 | + .map((e) => e.toString()) |
| 137 | + .indexOf(n.coordinate.toString()) |
| 138 | + .toString() |
| 139 | + : originalPatrolResult.path.has(n.coordinate.toString()) |
| 140 | + ? 'X' |
| 141 | + : originalPatrolResult.encounters.has(n.coordinate.toString()) |
| 142 | + ? 'E' |
| 143 | + : n.toString(), |
| 144 | + ); |
| 145 | + |
| 146 | + const result = originalPatrolResult.movement.filter((move) => { |
| 147 | + const pivotPlacement = enclosurePivotMap[move.direction.toString()]; |
| 148 | + if (!pivotPlacement) { |
| 149 | + throw new Error('invalid direction'); |
| 150 | + } |
| 151 | + return matchEnclosure( |
| 152 | + move.position, |
| 153 | + move.direction, |
| 154 | + pivotPlacement, |
| 155 | + boundingBox, |
| 156 | + obstructions, |
| 157 | + ); |
61 | 158 | }).length;
|
| 159 | + |
| 160 | + // const possibleObstructionCoordinates = [...originalPatrolResult.path].filter( |
| 161 | + // (p) => p !== guardNode.coordinate.toString(), |
| 162 | + // ); |
| 163 | + |
| 164 | + //const possibleObstructionCoordinates = [...freeNodes]; |
| 165 | + // |
| 166 | + //return possibleObstructionCoordinates.filter((possibleObstructionCoordinate) => { |
| 167 | + // const newObstructions = new Set(obstructions); |
| 168 | + // newObstructions.add(possibleObstructionCoordinate); |
| 169 | + // |
| 170 | + // const patrolResult = patrol(newObstructions, map, guardNode.coordinate); |
| 171 | + // |
| 172 | + // //g.print((n) => |
| 173 | + // // patrolResult.path.has(n.coordinate.toString()) |
| 174 | + // // ? 'X' |
| 175 | + // // : n.coordinate.toString() === possibleObstructionCoordinate |
| 176 | + // // ? 'O' |
| 177 | + // // : n.toString(), |
| 178 | + // //); |
| 179 | + // // console.log('--------', patrolResult); |
| 180 | + // return patrolResult.isLoop; |
| 181 | + //}).length; |
| 182 | + return result; |
62 | 183 | };
|
63 | 184 |
|
64 |
| -await task(p2, packageJson.aoc); // 4602 ~0.09ms |
| 185 | +await task(p2, packageJson.aoc); // 1563 ~0.09ms |
| 186 | +// 1563 too low |
0 commit comments