this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 2 done!

+131 -7
+76 -2
2023/day03/part2.ts
··· 7 7 console.log(answer(input)); 8 8 } 9 9 10 + type Coord = [number, number]; 11 + type PartNum = { num: number; coords: Coord[] }; 12 + type Symbol = { c: string; coord: Coord }; 13 + 10 14 export function answer(input: string): number { 11 - console.log(input); 12 - return 42; 15 + const grid = input.split("\n").map((l) => l.split("")); 16 + const [maybePartNums, symbols] = getPartsAndSymbols(grid); 17 + 18 + const partNums = maybePartNums.filter(({ coords }) => 19 + isSymbolAdjacent( 20 + coords, 21 + symbols.map((s) => s.coord), 22 + ), 23 + ); 24 + 25 + return symbols.reduce((sum, { c, coord }) => { 26 + if (c !== "*") return sum; 27 + 28 + const parts = partNums.filter(({ coords }) => 29 + isSymbolAdjacent(coords, [coord]), 30 + ); 31 + if (parts.length !== 2) return sum; 32 + 33 + const gearRatio = parts[0].num * parts[1].num; 34 + return sum + gearRatio; 35 + }, 0); 36 + } 37 + 38 + function isSymbolAdjacent(coords: Coord[], symbols: Coord[]): boolean { 39 + return coords.some(([x, y]) => 40 + adjacentCoords([x, y]).some(([ax, ay]) => 41 + symbols.some(([sx, sy]) => ax === sx && ay === sy), 42 + ), 43 + ); 44 + } 45 + 46 + function adjacentCoords([x, y]: Coord): Coord[] { 47 + return [ 48 + [x - 1, y - 1], 49 + [x + 1, y + 1], 50 + [x - 1, y + 1], 51 + [x + 1, y - 1], 52 + [x, y + 1], 53 + [x, y - 1], 54 + [x + 1, y], 55 + [x - 1, y], 56 + ]; 57 + } 58 + 59 + function getPartsAndSymbols(grid: string[][]): [PartNum[], Symbol[]] { 60 + const isSymbol = (c: string) => !c.match(/([0-9]|\.)/); 61 + const isNum = (c: string) => !!c.match(/[0-9]/); 62 + 63 + const partNums: PartNum[] = []; 64 + const symbols: Symbol[] = []; 65 + 66 + for (let y = 0; y < grid.length; y++) { 67 + for (let x = 0; x < grid[y].length; x++) { 68 + const c = grid[y][x]; 69 + if (isSymbol(c)) { 70 + symbols.push({ c, coord: [x, y] }); 71 + } else if (isNum(c)) { 72 + const coords: Coord[] = []; 73 + let numStr = ""; 74 + let current = c; 75 + while (current && isNum(current)) { 76 + coords.push([x, y]); 77 + numStr += current; 78 + current = grid[y][++x]; 79 + } 80 + x--; 81 + partNums.push({ num: parseInt(numStr), coords }); 82 + } 83 + } 84 + } 85 + 86 + return [partNums, symbols]; 13 87 }
+42 -3
2023/day03/puzzle.md
··· 33 33 34 34 Of course, the actual engine schematic is much larger. *What is the sum of all of the part numbers in the engine schematic?* 35 35 36 - To begin, [get your puzzle input](3/input). 36 + Your puzzle answer was `556057`. 37 + 38 + \--- Part Two --- 39 + ---------- 40 + 41 + The engineer finds the missing part and installs it in the engine! As the engine springs to life, you jump in the closest gondola, finally ready to ascend to the water source. 42 + 43 + You don't seem to be going very fast, though. Maybe something is still wrong? Fortunately, the gondola has a phone labeled "help", so you pick it up and the engineer answers. 44 + 45 + Before you can explain the situation, she suggests that you look out the window. There stands the engineer, holding a phone in one hand and waving with the other. You're going so slowly that you haven't even left the station. You exit the gondola. 46 + 47 + The missing part wasn't the only issue - one of the gears in the engine is wrong. A *gear* is any `*` symbol that is adjacent to *exactly two part numbers*. Its *gear ratio* is the result of multiplying those two numbers together. 48 + 49 + This time, you need to find the gear ratio of every gear and add them all up so that the engineer can figure out which gear needs to be replaced. 50 + 51 + Consider the same engine schematic again: 52 + 53 + ``` 54 + 467..114.. 55 + ...*...... 56 + ..35..633. 57 + ......#... 58 + 617*...... 59 + .....+.58. 60 + ..592..... 61 + ......755. 62 + ...$.*.... 63 + .664.598.. 64 + 65 + ``` 37 66 38 - Answer: 67 + In this schematic, there are *two* gears. The first is in the top left; it has part numbers `467` and `35`, so its gear ratio is `16345`. The second gear is in the lower right; its gear ratio is `451490`. (The `*` adjacent to `617` is *not* a gear because it is only adjacent to one part number.) Adding up all of the gear ratios produces `*467835*`. 39 68 40 - You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22Gear+Ratios%22+%2D+Day+3+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F3&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle. 69 + *What is the sum of all of the gear ratios in your engine schematic?* 70 + 71 + Your puzzle answer was `82824352`. 72 + 73 + Both parts of this puzzle are complete! They provide two gold stars: \*\* 74 + 75 + At this point, you should [return to your Advent calendar](/2023) and try another puzzle. 76 + 77 + If you still want to see it, you can [get your puzzle input](3/input). 78 + 79 + You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+%22Gear+Ratios%22+%2D+Day+3+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F3&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.
+13 -2
2023/day03/test.ts
··· 19 19 }); 20 20 21 21 Deno.test("part2", () => { 22 - const examples = ["abc", "def"].join("\n"); 23 - assertEquals(p2.answer(examples), 42); 22 + const examples = [ 23 + "467..114..", 24 + "...*......", 25 + "..35..633.", 26 + "......#...", 27 + "617*......", 28 + ".....+.58.", 29 + "..592.....", 30 + "......755.", 31 + "...$.*....", 32 + ".664.598..", 33 + ].join("\n"); 34 + assertEquals(p2.answer(examples), 467835); 24 35 });