Skip to content

Commit

Permalink
initial refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 10, 2023
1 parent 8e1eb84 commit 86c34e7
Showing 1 changed file with 28 additions and 70 deletions.
98 changes: 28 additions & 70 deletions src/2023/day10.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,38 @@ function solve(input) {
let max = 0;
while (queue.length > 0) {
const current = queue.shift();
max = Math.max(max, current.steps);
const neighbors = {
UP: { x: current.x, y: current.y - 1, steps: current.steps + 1 },
DOWN: { x: current.x, y: current.y + 1, steps: current.steps + 1 },
LEFT: { x: current.x - 1, y: current.y, steps: current.steps + 1 },
RIGHT: { x: current.x + 1, y: current.y, steps: current.steps + 1 },
};
const UP = { x: current.x, y: current.y - 1, steps: current.steps + 1 };
const DOWN = { x: current.x, y: current.y + 1, steps: current.steps + 1 };
const LEFT = { x: current.x - 1, y: current.y, steps: current.steps + 1 };
const RIGHT = { x: current.x + 1, y: current.y, steps: current.steps + 1 };
let next = [];
if (map[current.y][current.x] === 'S') {
let S = '';
if ('|7F'.includes(map[neighbors.UP.y]?.[neighbors.UP.x])) S += 'U';
if ('|LJ'.includes(map[neighbors.DOWN.y]?.[neighbors.DOWN.x])) S += 'D';
if ('-LF'.includes(map[neighbors.LEFT.y]?.[neighbors.LEFT.x])) S += 'L';
if ('-J7'.includes(map[neighbors.RIGHT.y]?.[neighbors.RIGHT.x])) S += 'R';
if (S === 'UD') S = '|';
else if (S === 'LR') S = '-';
else if (S === 'UR') S = 'L';
else if (S === 'UL') S = 'J';
else if (S === 'DL') S = '7';
else if (S === 'DR') S = 'F';
map[current.y][current.x] = S;
}
switch (map[current.y][current.x]) {
case 'S':
map[current.y][current.x] = '$';
if ('|7F'.includes(map[neighbors.UP.y]?.[neighbors.UP.x]))
next.push(neighbors.UP);
if ('|LJ'.includes(map[neighbors.DOWN.y]?.[neighbors.DOWN.x]))
next.push(neighbors.DOWN);
if ('-J7'.includes(map[neighbors.RIGHT.y]?.[neighbors.RIGHT.x]))
next.push(neighbors.RIGHT);
if ('-LF'.includes(map[neighbors.LEFT.y]?.[neighbors.LEFT.x]))
next.push(neighbors.LEFT);
break;
case '|':
next.push(neighbors.UP);
next.push(neighbors.DOWN);
break;
case '-':
next.push(neighbors.LEFT);
next.push(neighbors.RIGHT);
break;
case 'L':
next.push(neighbors.UP);
next.push(neighbors.RIGHT);
break;
case 'J':
next.push(neighbors.UP);
next.push(neighbors.LEFT);
break;
case '7':
next.push(neighbors.DOWN);
next.push(neighbors.LEFT);
break;
case 'F':
next.push(neighbors.DOWN);
next.push(neighbors.RIGHT);
break;
if ('|7F'.includes(map[UP.y]?.[UP.x])) S += 'U';
if ('|LJ'.includes(map[DOWN.y]?.[DOWN.x])) S += 'D';
if ('-LF'.includes(map[LEFT.y]?.[LEFT.x])) S += 'L';
if ('-J7'.includes(map[RIGHT.y]?.[RIGHT.x])) S += 'R';
if (S === 'UD') map[current.y][current.x] = '|';
if (S === 'LR') map[current.y][current.x] = '-';
if (S === 'UR') map[current.y][current.x] = 'L';
if (S === 'UL') map[current.y][current.x] = 'J';
if (S === 'DL') map[current.y][current.x] = '7';
if (S === 'DR') map[current.y][current.x] = 'F';
}
if (map[current.y][current.x] === '|') next.push(UP, DOWN);
if (map[current.y][current.x] === '-') next.push(LEFT, RIGHT);
if (map[current.y][current.x] === 'L') next.push(UP, RIGHT);
if (map[current.y][current.x] === 'J') next.push(UP, LEFT);
if (map[current.y][current.x] === '7') next.push(DOWN, LEFT);
if (map[current.y][current.x] === 'F') next.push(DOWN, RIGHT);
next = next.filter(({ x, y }) => !visited.has(`${x},${y}`));
next.forEach(({ x, y }) => visited.add(`${x},${y}`));
max = Math.max(max, current.steps);
queue.push(...next);
}
return { max, map, visited };
}

export function part1(input) {
const { max } = solve(input);
return max;
Expand All @@ -84,7 +51,7 @@ function zoomin(map) {
const line2 = [];
const line3 = [];
for (let xi = 0; xi < map[yi].length; xi++) {
if ('.OI'.includes(map[yi][xi])) {
if (map[yi][xi] === '.') {
line1.push('.', '.', '.');
line2.push('.', '.', '.');
line3.push('.', '.', '.');
Expand Down Expand Up @@ -112,8 +79,6 @@ function zoomin(map) {
line1.push('.', '.', '.');
line2.push('.', 'F', '-');
line3.push('.', '|', '.');
} else {
map[yi][xi];
}
}
bigger.push(line1, line2, line3);
Expand All @@ -134,12 +99,10 @@ function flood(map, x, y) {
{ x: current.x - 1, y: current.y },
{ x: current.x + 1, y: current.y },
];
neighbors.forEach(({ x, y }) => {
const next = neighbors.filter(({ x, y }) => {
if (!map[y] || !map[y][x]) trapped = false;
return map[y]?.[x] === '.' && !visited.has(`${x},${y}`);
});
let next = neighbors.filter(
({ x, y }) => map[y]?.[x] === '.' && !visited.has(`${x},${y}`),
);
next.forEach(({ x, y }) => visited.add(`${x},${y}`));
queue.push(...next);
}
Expand All @@ -148,7 +111,6 @@ function flood(map, x, y) {
if (visited.has(`${x},${y}`)) map[y][x] = trapped ? 'I' : 'O';
}
}
return trapped ? visited.size : 0;
}

export function part2(input) {
Expand All @@ -159,19 +121,15 @@ export function part2(input) {
}
}
const bigger = zoomin(map);
let sum = 0;
for (let y = 0; y < bigger.length; y++) {
for (let x = 0; x < bigger[y].length; x++) {
if (bigger[y][x] === '.') {
flood(bigger, x, y);
}
if (bigger[y][x] === '.') flood(bigger, x, y);
}
}
let sum = 0;
for (let y = 1; y < bigger.length; y += 3) {
for (let x = 1; x < bigger[y].length; x += 3) {
if (bigger[y][x] === 'I') {
sum++;
}
if (bigger[y][x] === 'I') sum++;
}
}
return sum;
Expand Down

0 comments on commit 86c34e7

Please sign in to comment.