Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 10, 2023
1 parent c779b3f commit b9a7853
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions src/2023/day10.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
function start(map, UP, DOWN, LEFT, RIGHT) {
let S = '';
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') return '|';
if (S === 'LR') return '-';
if (S === 'UR') return 'L';
if (S === 'UL') return 'J';
if (S === 'DL') return '7';
if (S === 'DR') return 'F';
}

function solve(input) {
const map = input.split('\n').map(line => line.split(''));
const y = map.findIndex(line => line.includes('S'));
const x = map[y].findIndex(c => c === 'S');
const queue = [{ x, y, steps: 0 }];
let visited = new Set();
let visited = new Set([`${x},${y}`]);
let max = 0;
while (queue.length > 0) {
const current = queue.shift();
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[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';
map[current.y][current.x] = start(map, UP, DOWN, LEFT, RIGHT);
}
let next = [];
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);
Expand All @@ -39,13 +43,8 @@ function solve(input) {
return { max, map, visited };
}

export function part1(input) {
const { max } = solve(input);
return max;
}

function zoomin(map) {
let bigger = [];
const big = [];
for (let yi = 0; yi < map.length; yi++) {
const line1 = [];
const line2 = [];
Expand Down Expand Up @@ -81,16 +80,25 @@ function zoomin(map) {
line3.push('.', '|', '.');
}
}
bigger.push(line1, line2, line3);
big.push(line1, line2, line3);
}
return big;
}

function zoomout(map) {
const small = [];
for (let y = 0; y < map.length; y += 3) {
const line = [];
for (let x = 0; x < map[y].length; x += 3) line.push(map[y + 1][x + 1]);
small.push(line);
}
return bigger;
return small;
}

function flood(map, x, y) {
let visited = new Set();
let visited = new Set([`${x},${y}`]);
let queue = [{ x, y }];
let trapped = true;
visited.add(`${x},${y}`);
while (queue.length > 0) {
const current = queue.shift();
const neighbors = [
Expand All @@ -113,24 +121,22 @@ function flood(map, x, y) {
}
}

export function part1(input) {
const { max } = solve(input);
return max;
}

export function part2(input) {
const { map, visited } = solve(input);
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
if (!visited.has(`${x},${y}`)) map[y][x] = '.';
}
}
const bigger = zoomin(map);
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);
}
}
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++;
let { map, visited } = solve(input);
map = map.map((line, y) => {
return line.map((c, x) => (visited.has(`${x},${y}`) ? c : '.'));
});
const big = zoomin(map);
for (let y = 0; y < big.length; y++) {
for (let x = 0; x < big[y].length; x++) {
if (big[y][x] === '.') flood(big, x, y);
}
}
return sum;
const small = zoomout(big);
return small.flat().filter(c => c === 'I').length;
}

0 comments on commit b9a7853

Please sign in to comment.