|
| 1 | +import { readFile } from 'node:fs/promises'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +async function main() { |
| 5 | + const data = await readFile( |
| 6 | + resolve('../../../../', 'data', '2024-06.txt'), |
| 7 | + 'utf8' |
| 8 | + ); |
| 9 | + |
| 10 | + // Part 1 |
| 11 | + { |
| 12 | + console.time('Part 1'); |
| 13 | + const result = part1(data); |
| 14 | + console.info('Result:', result); |
| 15 | + console.timeEnd('Part 1'); |
| 16 | + } |
| 17 | + |
| 18 | + // Part 2 |
| 19 | + { |
| 20 | + console.time('Part 2'); |
| 21 | + const result = part2(data); |
| 22 | + console.info('Result:', result); |
| 23 | + console.timeEnd('Part 2'); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +main(); |
| 28 | + |
| 29 | +export function part1(data: string) { |
| 30 | + const lines = data.trim().split('\n'); |
| 31 | + // @ts-ignore |
| 32 | + let guardPos: { |
| 33 | + x: number; |
| 34 | + y: number; |
| 35 | + direction: 'up' | 'down' | 'left' | 'right'; |
| 36 | + } = {}; |
| 37 | + |
| 38 | + let sum = 0; |
| 39 | + const seen = new Set(); |
| 40 | + |
| 41 | + const grid = lines.map((line, rowIdx) => { |
| 42 | + return line.split('').map((col, colIdx) => { |
| 43 | + if (col === '^') { |
| 44 | + guardPos = { |
| 45 | + x: colIdx, |
| 46 | + y: rowIdx, |
| 47 | + direction: 'up', |
| 48 | + }; |
| 49 | + } |
| 50 | + return col; |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + seen.add(`${guardPos.x}-${guardPos.y}`); |
| 55 | + sum += 1; |
| 56 | + |
| 57 | + while (true) { |
| 58 | + if ( |
| 59 | + guardPos.y < 0 || |
| 60 | + guardPos.y >= grid.length || |
| 61 | + guardPos.x < 0 || |
| 62 | + guardPos.x >= grid[0].length |
| 63 | + ) { |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + switch (guardPos.direction) { |
| 68 | + case 'up': |
| 69 | + if (grid?.[guardPos.y - 1]?.[guardPos.x] !== '#') { |
| 70 | + guardPos.y -= 1; |
| 71 | + |
| 72 | + if (!seen.has(`${guardPos.x}-${guardPos.y}`)) { |
| 73 | + seen.add(`${guardPos.x}-${guardPos.y}`); |
| 74 | + sum += 1; |
| 75 | + } |
| 76 | + } else { |
| 77 | + guardPos.direction = 'right'; |
| 78 | + } |
| 79 | + break; |
| 80 | + case 'right': |
| 81 | + if (grid?.[guardPos.y]?.[guardPos.x + 1] !== '#') { |
| 82 | + guardPos.x += 1; |
| 83 | + |
| 84 | + if (!seen.has(`${guardPos.x}-${guardPos.y}`)) { |
| 85 | + seen.add(`${guardPos.x}-${guardPos.y}`); |
| 86 | + sum += 1; |
| 87 | + } |
| 88 | + } else { |
| 89 | + guardPos.direction = 'down'; |
| 90 | + } |
| 91 | + break; |
| 92 | + case 'down': |
| 93 | + if (grid?.[guardPos.y + 1]?.[guardPos.x] !== '#') { |
| 94 | + guardPos.y += 1; |
| 95 | + |
| 96 | + if (!seen.has(`${guardPos.x}-${guardPos.y}`)) { |
| 97 | + seen.add(`${guardPos.x}-${guardPos.y}`); |
| 98 | + sum += 1; |
| 99 | + } |
| 100 | + } else { |
| 101 | + guardPos.direction = 'left'; |
| 102 | + } |
| 103 | + break; |
| 104 | + case 'left': |
| 105 | + if (grid?.[guardPos.y]?.[guardPos.x - 1] !== '#') { |
| 106 | + guardPos.x -= 1; |
| 107 | + |
| 108 | + if (!seen.has(`${guardPos.x}-${guardPos.y}`)) { |
| 109 | + seen.add(`${guardPos.x}-${guardPos.y}`); |
| 110 | + sum += 1; |
| 111 | + } |
| 112 | + } else { |
| 113 | + guardPos.direction = 'up'; |
| 114 | + } |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return sum - 1; |
| 120 | +} |
| 121 | + |
| 122 | +export function part2(data: string) {} |
0 commit comments