-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.ts
165 lines (138 loc) · 5.28 KB
/
day03.ts
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
import fs from 'fs';
const DAY = '03';
interface LocationData {
type: 'partNumber' | 'symbol';
value: string | number;
coordinates: {
x: number;
y: number;
};
}
async function part1(fileName: string) {
const input = fs.readFileSync(`input/${fileName}`);
const rows = input.toString().split('\r\n');
const partNumbers: number[] = [];
for (let i = 0; i < rows.length; i++) {
const rowsToCheck: string[] = [];
rowsToCheck.push(rows[i]); // Current row
if (i > 0) rowsToCheck.push(rows[i - 1]); // Previous row
if (i < rows.length - 1) rowsToCheck.push(rows[i + 1]); // Next row
let currentNumberIndexes: number[] = [];
for (let j = 0; j < rows[i].length; j++) {
if (!isNumber(rows[i][j])) {
if (currentNumberIndexes.length) {
if (checkForSymbols(rowsToCheck, [Math.min(...currentNumberIndexes), Math.max(...currentNumberIndexes)])) {
partNumbers.push(parseInt(rows[i].substring(Math.min(...currentNumberIndexes), Math.max(...currentNumberIndexes) + 1)));
}
currentNumberIndexes = [];
}
continue;
} else {
currentNumberIndexes.push(j);
}
}
if (currentNumberIndexes.length) {
if (checkForSymbols(rowsToCheck, [Math.min(...currentNumberIndexes), Math.max(...currentNumberIndexes)])) {
partNumbers.push(parseInt(rows[i].substring(Math.min(...currentNumberIndexes), Math.max(...currentNumberIndexes) + 1)));
}
}
}
console.log(partNumbers.reduce((acc, cur) => acc + cur, 0));
}
function isNumber(char: string): boolean {
return !Number.isNaN(parseInt(char));
}
function isSymbol(char: string): boolean {
return !isNumber(char) && char !== '.';
}
function checkForSymbols(rows: string[], indexRange: number[]): boolean {
const currentRow = rows[0];
const charsToCheck: string[] = [];
const firstIndex = Math.max(indexRange[0] - 1, 0);
const lastIndex = Math.min(indexRange[1] + 1, rows[0].length - 1);
charsToCheck.push(currentRow[firstIndex]);
charsToCheck.push(currentRow[lastIndex]);
for (const row of rows.slice(1)) {
charsToCheck.push(...row.substring(firstIndex, lastIndex + 1));
}
for (const char of charsToCheck) {
if (isSymbol(char)) return true;
}
return false;
}
function calculateGearRatio(potenialGear: LocationData, parts: LocationData[]): number | undefined {
// console.log(`Checking potenial gear with coords ${potenialGear.coordinates.y}, ${potenialGear.coordinates.x}`);
const nearByParts: LocationData[] = [];
const accetableXCoords = [potenialGear.coordinates.x, potenialGear.coordinates.x + 1, potenialGear.coordinates.x - 1];
const partsToCheck = parts.filter((part) => {
if (
(part.coordinates.y === potenialGear.coordinates.y ||
part.coordinates.y === potenialGear.coordinates.y - 1 ||
part.coordinates.y === potenialGear.coordinates.y + 1) &&
(part.coordinates.x === potenialGear.coordinates.x ||
part.coordinates.x === potenialGear.coordinates.x + 1 ||
part.coordinates.x === potenialGear.coordinates.x - 1 ||
part.coordinates.x === potenialGear.coordinates.x - 2 ||
part.coordinates.x === potenialGear.coordinates.x - 3)
)
return true;
});
for (const part of partsToCheck) {
// Add all the coords that the part takes up on x axis
const partXCoords = [part.coordinates.x];
if (part.value.toString().length > 1) partXCoords.push(part.coordinates.x + 1);
if (part.value.toString().length > 2) partXCoords.push(part.coordinates.x + 2);
// Check if the part is in acceptable range of the potential gear
for (const partXCoord of partXCoords) {
if (accetableXCoords.includes(partXCoord)) {
nearByParts.push(part);
break;
}
}
}
if (nearByParts.length === 2) return nearByParts.reduce((acc, cur) => acc * (cur.value as number), 1);
else return undefined;
}
async function part2(fileName: string) {
const input = fs.readFileSync(`input/${fileName}`);
const rows = input.toString().split('\r\n');
const locationData: LocationData[] = [];
for (let y = 0; y < rows.length; y++) {
let previousCharWasNumber = false;
for (let x = 0; x < rows[y].length; x++) {
if (rows[y][x] === '.') {
previousCharWasNumber = false;
continue;
}
if (!!rows[y][x].match(/\d+/) && !previousCharWasNumber) {
previousCharWasNumber = true;
locationData.push({
type: 'partNumber',
value: parseInt(rows[y].substring(x).match(/\d+/)![0]),
coordinates: { x, y }
});
} else if (!rows[y][x].match(/\d+/)) {
previousCharWasNumber = false;
locationData.push({
type: 'symbol',
value: rows[y][x],
coordinates: { x, y }
});
}
}
}
const parts = locationData.filter((data) => data.type === 'partNumber');
const potenialGears = locationData.filter((data) => data.value === '*');
const gearRatios: number[] = [];
for (const potenialGear of potenialGears) {
const gearRatio = calculateGearRatio(potenialGear, parts);
if (gearRatio) {
gearRatios.push(gearRatio);
}
}
console.log(gearRatios.reduce((acc, cur) => acc + cur, 0));
}
// part1(`day${DAY}_example.txt`);
// part1(`day${DAY}.txt`);
// part2(`day${DAY}_example.txt`);
part2(`day${DAY}.txt`);