From 86c34e7e494bc8e0e331950bf87cc6733c3ba6a9 Mon Sep 17 00:00:00 2001 From: Shahar Talmi Date: Sun, 10 Dec 2023 15:57:34 +0200 Subject: [PATCH] initial refactoring --- src/2023/day10.js | 98 ++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 70 deletions(-) diff --git a/src/2023/day10.js b/src/2023/day10.js index 77265c0e..424cf897 100644 --- a/src/2023/day10.js +++ b/src/2023/day10.js @@ -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; @@ -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('.', '.', '.'); @@ -112,8 +79,6 @@ function zoomin(map) { line1.push('.', '.', '.'); line2.push('.', 'F', '-'); line3.push('.', '|', '.'); - } else { - map[yi][xi]; } } bigger.push(line1, line2, line3); @@ -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); } @@ -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) { @@ -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;