2022 - Day 8
This commit is contained in:
129
2022/08/solution.ts
Normal file
129
2022/08/solution.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
const sample = await Deno.readTextFile("sample.txt");
|
||||
const input = await Deno.readTextFile("input.txt");
|
||||
|
||||
/**** PART 1 ********************************/
|
||||
|
||||
const solvePart1 = (data: string): number => {
|
||||
const grid = data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => line.split("").filter(Boolean).map(parseFloat));
|
||||
|
||||
let counter = 0;
|
||||
|
||||
for (let y = 0; y < grid.length; y += 1) {
|
||||
for (let x = 0; x < grid[0].length; x += 1) {
|
||||
const height = grid[y][x];
|
||||
let visibleUntil = true;
|
||||
let visibleFrom = true;
|
||||
|
||||
/* Detect view on Y */
|
||||
let verticalView = true;
|
||||
for (let y1 = 0; y1 < grid.length; y1 += 1) {
|
||||
if (y1 < y) {
|
||||
visibleUntil = visibleUntil && grid[y1][x] < height;
|
||||
}
|
||||
if (y1 > y) {
|
||||
visibleFrom = visibleFrom && grid[y1][x] < height;
|
||||
}
|
||||
}
|
||||
verticalView = verticalView && (visibleUntil || visibleFrom);
|
||||
|
||||
/* Detect view on X */
|
||||
let horizontalView = true;
|
||||
visibleUntil = true;
|
||||
visibleFrom = true;
|
||||
for (let x1 = 0; x1 < grid.length; x1 += 1) {
|
||||
if (x1 < x) {
|
||||
visibleUntil = visibleUntil && grid[y][x1] < height;
|
||||
}
|
||||
if (x1 > x) {
|
||||
visibleFrom = visibleFrom && grid[y][x1] < height;
|
||||
}
|
||||
}
|
||||
horizontalView = horizontalView && (visibleUntil || visibleFrom);
|
||||
if (horizontalView || verticalView) {
|
||||
counter = counter + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
};
|
||||
|
||||
console.log("Sample:", solvePart1(sample)); // 21
|
||||
console.log("Input", solvePart1(input)); // 1851
|
||||
|
||||
/**** PART 2 ********************************/
|
||||
|
||||
const solvePart2 = (data: string): number => {
|
||||
const grid = data
|
||||
.split("\n")
|
||||
.filter(Boolean)
|
||||
.map((line) => line.split("").filter(Boolean).map(parseFloat));
|
||||
|
||||
let highestCounter = 0;
|
||||
for (let y = 0; y < grid.length; y += 1) {
|
||||
const row = grid[y];
|
||||
for (let x = 0; x < row.length; x += 1) {
|
||||
let counter = 1;
|
||||
const height = row[x];
|
||||
|
||||
let yCounter = 0;
|
||||
for (let y2 = y + 1; y2 < grid.length; y2 += 1) {
|
||||
const treeHeight = grid[y2][x];
|
||||
if (treeHeight === undefined) {
|
||||
break;
|
||||
}
|
||||
yCounter += 1;
|
||||
if (treeHeight >= height) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
counter = yCounter;
|
||||
yCounter = 0;
|
||||
for (let y2 = y - 1; y2 >= 0; y2 -= 1) {
|
||||
const treeHeight = grid[y2][x];
|
||||
if (treeHeight === undefined) {
|
||||
break;
|
||||
}
|
||||
yCounter += 1;
|
||||
if (treeHeight >= height) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
counter *= yCounter;
|
||||
let xCounter = 0;
|
||||
for (let x2 = x + 1; x2 < row.length; x2 += 1) {
|
||||
const treeHeight = grid[y][x2];
|
||||
if (treeHeight === undefined) {
|
||||
break;
|
||||
}
|
||||
xCounter += 1;
|
||||
if (treeHeight >= height) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
counter *= xCounter;
|
||||
xCounter = 0;
|
||||
for (let x2 = x - 1; x2 >= 0; x2 -= 1) {
|
||||
const treeHeight = grid[y][x2];
|
||||
if (treeHeight === undefined) {
|
||||
break;
|
||||
}
|
||||
xCounter += 1;
|
||||
if (treeHeight >= height) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
counter *= xCounter;
|
||||
if (counter > highestCounter) {
|
||||
highestCounter = counter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return highestCounter;
|
||||
};
|
||||
|
||||
console.log("Sample:", solvePart2(sample)); // 8
|
||||
console.log("Input", solvePart2(input)); // 574080
|
||||
Reference in New Issue
Block a user