Day 12 Part 2
This commit is contained in:
@@ -20,34 +20,46 @@ function getAllAdjacentPlots({ x, y }: Coord) {
|
|||||||
|
|
||||||
let adj: Array<Plot> = [plot];
|
let adj: Array<Plot> = [plot];
|
||||||
|
|
||||||
let firstOneWasAnEdge = false;
|
let adjLetters: Array<string> = [];
|
||||||
let lastOneWasAnEdge = false;
|
|
||||||
[
|
[
|
||||||
{ x, y: y - 1 },
|
{ x, y: y - 1 },
|
||||||
|
{ x: x + 1, y: y - 1 },
|
||||||
{ x: x + 1, y },
|
{ x: x + 1, y },
|
||||||
|
{ x: x + 1, y: y + 1 },
|
||||||
{ x, y: y + 1 },
|
{ x, y: y + 1 },
|
||||||
|
{ x: x - 1, y: y + 1 },
|
||||||
{ x: x - 1, y },
|
{ x: x - 1, y },
|
||||||
|
{ x: x - 1, y: y - 1 },
|
||||||
].forEach((adjCoord, i) => {
|
].forEach((adjCoord, i) => {
|
||||||
const adjLetter = plots[adjCoord.y]
|
const adjLetter = plots[adjCoord.y]
|
||||||
&& plots[adjCoord.y][adjCoord.x]
|
|
||||||
&& plots[adjCoord.y][adjCoord.x];
|
&& plots[adjCoord.y][adjCoord.x];
|
||||||
|
|
||||||
|
if (i % 2 === 0) {
|
||||||
if (adjLetter === letter) {
|
if (adjLetter === letter) {
|
||||||
adj = [...adj, ...getAllAdjacentPlots(adjCoord)];
|
adj = [...adj, ...getAllAdjacentPlots(adjCoord)];
|
||||||
lastOneWasAnEdge = false;
|
|
||||||
} else if (adjLetter !== '.' + letter) {
|
} else if (adjLetter !== '.' + letter) {
|
||||||
plot.edges++;
|
plot.edges++;
|
||||||
if (lastOneWasAnEdge) {
|
|
||||||
plot.corners++;
|
|
||||||
}
|
}
|
||||||
lastOneWasAnEdge = true;
|
|
||||||
}
|
|
||||||
if (i === 0) {
|
|
||||||
firstOneWasAnEdge = lastOneWasAnEdge;
|
|
||||||
}
|
}
|
||||||
|
adjLetters.push(adjLetter ? adjLetter.replace('.', '') : '_');
|
||||||
});
|
});
|
||||||
if (firstOneWasAnEdge && lastOneWasAnEdge) {
|
|
||||||
|
for (let i = 1; i < 8; i += 2) {
|
||||||
|
let cornerLetter = adjLetters[i];
|
||||||
|
let beforeLetter = adjLetters[i - 1];
|
||||||
|
let afterLetter = adjLetters[i + 1] || adjLetters[0];
|
||||||
|
if (cornerLetter === letter && (beforeLetter === letter || afterLetter === letter)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
(beforeLetter === letter && afterLetter === letter)
|
||||||
|
|| (beforeLetter !== letter && afterLetter !== letter)
|
||||||
|
) {
|
||||||
plot.corners++;
|
plot.corners++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return adj;
|
return adj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +75,6 @@ for (let y = 0; y < plots.length; y++) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(regions);
|
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
|
|
||||||
regions.forEach((plots) => {
|
regions.forEach((plots) => {
|
||||||
@@ -77,4 +88,3 @@ regions.forEach((plots) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log(sum);
|
console.log(sum);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user