-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.js
238 lines (212 loc) Β· 5.99 KB
/
03.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput, sum} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const engineSchematic = input
.trim()
.split('\n')
.map(line => [...line]);
function isCharacterSymbol(character) {
if (Number.isInteger(Number(character))) return false;
return character !== '.';
}
function isNumberAdjacentToSymbol(xPosition, yPosition) {
// Check direct neighbors
// Check left
if (
xPosition > 0 &&
isCharacterSymbol(engineSchematic[yPosition][xPosition - 1])
)
return true;
// Check right
if (
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterSymbol(engineSchematic[yPosition][xPosition + 1])
)
return true;
// Check above
if (
yPosition > 0 &&
isCharacterSymbol(engineSchematic[yPosition - 1][xPosition])
)
return true;
// Check below
if (
yPosition < engineSchematic.length - 1 &&
isCharacterSymbol(engineSchematic[yPosition + 1][xPosition])
)
return true;
// Check diagonally neighbors
// Check top left
if (
yPosition > 0 &&
xPosition > 0 &&
isCharacterSymbol(engineSchematic[yPosition - 1][xPosition - 1])
)
return true;
// Check top right
if (
yPosition > 0 &&
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterSymbol(engineSchematic[yPosition - 1][xPosition + 1])
)
return true;
// Check bottom left
if (
yPosition < engineSchematic.length - 1 &&
xPosition > 0 &&
isCharacterSymbol(engineSchematic[yPosition + 1][xPosition - 1])
)
return true;
// Check bottom right
if (
yPosition < engineSchematic.length - 1 &&
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterSymbol(engineSchematic[yPosition + 1][xPosition + 1])
)
return true;
return false;
}
function isCharacterAsterisk(character) {
if (Number.isInteger(Number(character))) return false;
return character === '*';
}
function getAdjacentAsterisks(xPosition, yPosition) {
const adjacentAsterisks = [];
// Check direct neighbors
// Check left
if (
xPosition > 0 &&
isCharacterAsterisk(engineSchematic[yPosition][xPosition - 1])
) {
adjacentAsterisks.push({x: xPosition - 1, y: yPosition});
}
// Check right
if (
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterAsterisk(engineSchematic[yPosition][xPosition + 1])
) {
adjacentAsterisks.push({x: xPosition + 1, y: yPosition});
}
// Check above
if (
yPosition > 0 &&
isCharacterAsterisk(engineSchematic[yPosition - 1][xPosition])
) {
adjacentAsterisks.push({x: xPosition, y: yPosition - 1});
}
// Check below
if (
yPosition < engineSchematic.length - 1 &&
isCharacterAsterisk(engineSchematic[yPosition + 1][xPosition])
) {
adjacentAsterisks.push({x: xPosition, y: yPosition + 1});
}
// Check diagonally neighbors
// Check top left
if (
yPosition > 0 &&
xPosition > 0 &&
isCharacterAsterisk(engineSchematic[yPosition - 1][xPosition - 1])
) {
adjacentAsterisks.push({x: xPosition - 1, y: yPosition - 1});
}
// Check top right
if (
yPosition > 0 &&
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterAsterisk(engineSchematic[yPosition - 1][xPosition + 1])
) {
adjacentAsterisks.push({x: xPosition + 1, y: yPosition - 1});
}
// Check bottom left
if (
yPosition < engineSchematic.length - 1 &&
xPosition > 0 &&
isCharacterAsterisk(engineSchematic[yPosition + 1][xPosition - 1])
) {
adjacentAsterisks.push({x: xPosition - 1, y: yPosition + 1});
}
// Check bottom right
if (
yPosition < engineSchematic.length - 1 &&
xPosition < engineSchematic[yPosition].length - 1 &&
isCharacterAsterisk(engineSchematic[yPosition + 1][xPosition + 1])
) {
adjacentAsterisks.push({x: xPosition + 1, y: yPosition + 1});
}
return adjacentAsterisks;
}
const partNumbers = [];
for (const [yPosition, line] of engineSchematic.entries()) {
let currentNumber;
let isCurrentNumberAdjacentToSymbol = false;
for (const [xPosition, character] of line.entries()) {
if (Number.isInteger(Number(character))) {
currentNumber = `${currentNumber || ''}${character}`;
const isCurrentCharacterAdjacentToSymbol = isNumberAdjacentToSymbol(
xPosition,
yPosition,
);
if (isCurrentCharacterAdjacentToSymbol) {
isCurrentNumberAdjacentToSymbol = true;
}
continue;
}
if (currentNumber) {
if (isCurrentNumberAdjacentToSymbol) {
partNumbers.push(Number(currentNumber));
}
currentNumber = undefined;
isCurrentNumberAdjacentToSymbol = false;
}
}
if (currentNumber && isCurrentNumberAdjacentToSymbol) {
partNumbers.push(Number(currentNumber));
currentNumber = undefined;
isCurrentNumberAdjacentToSymbol = false;
}
}
const sumOfPartNumbers = sum(partNumbers);
console.log('Sum of part numbers:', sumOfPartNumbers);
const gears = {};
for (const [yPosition, line] of engineSchematic.entries()) {
let currentNumber;
let adjacentAsterisks = [];
for (const [xPosition, character] of line.entries()) {
if (Number.isInteger(Number(character))) {
currentNumber = `${currentNumber || ''}${character}`;
const currentAdjacentAsterisks = getAdjacentAsterisks(
xPosition,
yPosition,
);
adjacentAsterisks.push(...currentAdjacentAsterisks);
continue;
}
if (currentNumber) {
for (const adjacentAsterisk of adjacentAsterisks) {
const index = `${adjacentAsterisk.x}-${adjacentAsterisk.y}`;
gears[index] = gears[index] || new Set();
gears[index].add(Number(currentNumber));
}
currentNumber = undefined;
adjacentAsterisks = [];
}
}
if (currentNumber) {
for (const adjacentAsterisk of adjacentAsterisks) {
const index = `${adjacentAsterisk.x}-${adjacentAsterisk.y}`;
gears[index] = gears[index] || new Set();
gears[index].add(Number(currentNumber));
}
currentNumber = undefined;
adjacentAsterisks = [];
}
}
const gearRatios = Object.values(gears)
.filter(gear => gear.size === 2)
.map(gear => [...gear])
.map(gear => gear[0] * gear[1]);
const sumGearRatios = sum(gearRatios);
console.log('Sum gear ratios:', sumGearRatios);