Skip to content

Commit 53e2bfa

Browse files
committed
12/2024 (part 2)
1 parent affb320 commit 53e2bfa

File tree

2 files changed

+111
-5
lines changed

2 files changed

+111
-5
lines changed

2024/12/index.ts

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const directions = {
1616
LEFT: { x: -1, y: 0 },
1717
} satisfies Record<string, Coordinate>;
1818

19+
const diagonalDirections = {
20+
RIGHT_UP: { x: 1, y: -1 },
21+
RIGHT_DOWN: { x: 1, y: 1 },
22+
LEFT_DOWN: { x: -1, y: 1 },
23+
LEFT_UP: { x: -1, y: -1 },
24+
} satisfies Record<string, Coordinate>;
25+
1926
const getCellKey = (cell: Coordinate) => `${cell.x},${cell.y}`;
2027

2128
const getCellFromKey = (key: string) => {
@@ -124,7 +131,106 @@ const getRegions = () => {
124131
})();
125132

126133
// Part 2
127-
// (() => {
128-
// console.time("part 2");
129-
// console.timeEnd("part 2");
130-
// })();
134+
(() => {
135+
console.time("part 2");
136+
const regions = getRegions();
137+
let totalPrice = 0;
138+
139+
for (const [, region] of regions) {
140+
let corners = 0;
141+
142+
if (region.cells.size === 1) {
143+
totalPrice += 4;
144+
continue;
145+
}
146+
147+
for (const cellKey of region.cells) {
148+
const cell = getCellFromKey(cellKey);
149+
150+
/* **************
151+
* OUTER CORNERS
152+
* **************/
153+
154+
const upCellKey = getCellKey(move(cell, directions.UP));
155+
const rightCellKey = getCellKey(move(cell, directions.RIGHT));
156+
const downCellKey = getCellKey(move(cell, directions.DOWN));
157+
const leftCellKey = getCellKey(move(cell, directions.LEFT));
158+
159+
// Outer top-left corner
160+
if (!region.cells.has(leftCellKey) && !region.cells.has(upCellKey)) {
161+
corners++;
162+
}
163+
164+
// Outer top-right corner
165+
if (!region.cells.has(upCellKey) && !region.cells.has(rightCellKey)) {
166+
corners++;
167+
}
168+
169+
// Outer bottom-right corner
170+
if (!region.cells.has(rightCellKey) && !region.cells.has(downCellKey)) {
171+
corners++;
172+
}
173+
174+
// Outer bottom-left corner
175+
if (!region.cells.has(downCellKey) && !region.cells.has(leftCellKey)) {
176+
corners++;
177+
}
178+
179+
/* **************
180+
* INNER CORNERS
181+
* **************/
182+
183+
const rightUpCellKey = getCellKey(
184+
move(cell, diagonalDirections.RIGHT_UP)
185+
);
186+
const rightDownCellKey = getCellKey(
187+
move(cell, diagonalDirections.RIGHT_DOWN)
188+
);
189+
const leftDownCellKey = getCellKey(
190+
move(cell, diagonalDirections.LEFT_DOWN)
191+
);
192+
const leftUpCellKey = getCellKey(move(cell, diagonalDirections.LEFT_UP));
193+
194+
// Inner top-left corner
195+
if (
196+
region.cells.has(rightCellKey) &&
197+
region.cells.has(downCellKey) &&
198+
!region.cells.has(rightDownCellKey)
199+
) {
200+
corners++;
201+
}
202+
203+
// Inner top-right corner
204+
if (
205+
region.cells.has(leftCellKey) &&
206+
region.cells.has(downCellKey) &&
207+
!region.cells.has(leftDownCellKey)
208+
) {
209+
corners++;
210+
}
211+
212+
// Inner bottom-right corner
213+
if (
214+
region.cells.has(leftCellKey) &&
215+
region.cells.has(upCellKey) &&
216+
!region.cells.has(leftUpCellKey)
217+
) {
218+
corners++;
219+
}
220+
221+
// Inner bottom-left corner
222+
if (
223+
region.cells.has(rightCellKey) &&
224+
region.cells.has(upCellKey) &&
225+
!region.cells.has(rightUpCellKey)
226+
) {
227+
corners++;
228+
}
229+
}
230+
231+
totalPrice += region.cells.size * corners;
232+
}
233+
234+
console.log("part 2 totalPrice ::", totalPrice);
235+
console.timeEnd("part 2");
236+
})();

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :----------------------------------------: | :----: | :----: |
9-
| [12](https://adventofcode.com/2024/day/12) || - |
9+
| [12](https://adventofcode.com/2024/day/12) || |
1010
| [11](https://adventofcode.com/2024/day/11) |||
1111
| [10](https://adventofcode.com/2024/day/10) |||
1212
| [09](https://adventofcode.com/2024/day/9) |||

0 commit comments

Comments
 (0)