Skip to content

Commit e34399d

Browse files
committed
Day 3 part 2 solution
1 parent a8170d8 commit e34399d

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

2023/Day03/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Day 3: Gear Ratios
2+
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
3+
4+
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
5+
6+
"Aaah!"
7+
8+
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
9+
10+
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
11+
12+
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
13+
14+
Here is an example engine schematic:
15+
16+
```
17+
467..114..
18+
...*......
19+
..35..633.
20+
......#...
21+
617*......
22+
.....+.58.
23+
..592.....
24+
......755.
25+
...$.*....
26+
.664.598..
27+
```
28+
29+
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
30+
31+
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
32+
33+
## Part Two
34+
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.
35+
36+
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.
37+
38+
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.
39+
40+
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.
41+
42+
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.
43+
44+
Consider the same engine schematic again:
45+
46+
```
47+
467..114..
48+
...*......
49+
..35..633.
50+
......#...
51+
617*......
52+
.....+.58.
53+
..592.....
54+
......755.
55+
...$.*....
56+
.664.598..
57+
```
58+
59+
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.
60+
61+
What is the sum of all of the gear ratios in your engine schematic?

2023/Day03/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,36 @@ for (let i = 0; i < lines.length; i++) {
9090
// Add up the part numbers
9191
const sumOfPartNumbers = numbersWithAdjacentSymbols.reduce((sum, obj) => sum + obj.value, 0);
9292

93-
console.log(sumOfPartNumbers);
93+
console.log(sumOfPartNumbers);
94+
95+
/*----- Part Two ----- */
96+
97+
// group an array of objects by a specific property
98+
const groupBy = (array, property) => {
99+
const groups = {};
100+
array.forEach((item) => {
101+
const key = JSON.stringify(item[property]);
102+
groups[key] = groups[key] || [];
103+
groups[key].push(item);
104+
});
105+
return groups;
106+
}
107+
108+
const groupedByAdjacentSymbols = groupBy(numbersWithAdjacentSymbols, 'adjacentSymbols');
109+
110+
// find gears with the same adjacentSymbols object when the symbol equals '*'
111+
const gearsArray = Object.values(groupedByAdjacentSymbols)
112+
.filter((parts) => parts.length > 1 && parts.some(part => part.adjacentSymbols[0].value === '*'))
113+
.reduce((gears, parts) => {
114+
const numbers = parts.map(part => part.value);
115+
gears.push(numbers);
116+
return gears;
117+
}, []);
118+
119+
// Get the gear ratio by multiplying the numbers together
120+
const gearRatios = gearsArray.map(array => array.reduce((acc, value) => acc * value, 1));
121+
122+
// Get the sum of all gear ratios
123+
const sumOfGearRatios = gearRatios.reduce((acc, value) => acc + value, 0);
124+
125+
console.log(sumOfGearRatios);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy