Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 11, 2023
1 parent d218162 commit 5546585
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/2023/day11.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ export function part1(input, add = 1) {
const map = input.split('\n').map(line => line.split(''));
let stars = map
.flatMap((row, y) => row.map((cell, x) => ({ cell, x, y })))
.filter(({ cell }) => cell === '#')
.map((c, i) => ({ ...c, id: i + 1 }));
.filter(({ cell }) => cell === '#');
let ydiff = 0;
for (let y = 0; y < map.length; y++) {
if (map[y].every(c => c === '.')) {
Expand All @@ -18,16 +17,12 @@ export function part1(input, add = 1) {
xdiff += add;
}
}
const length = stars
.map((cell, i) => {
return stars.slice(i + 1).reduce((acc, other) => {
const distance =
Math.abs(cell.x - other.x) + Math.abs(cell.y - other.y);
return acc + distance;
}, 0);
})
.reduce((a, b) => a + b);
return length;
const distances = stars.map((a, i) =>
stars
.slice(i + 1)
.reduce((acc, b) => acc + Math.abs(a.x - b.x) + Math.abs(a.y - b.y), 0),
);
return distances.reduce((a, b) => a + b);
}

export function part2(input) {
Expand Down

0 comments on commit 5546585

Please sign in to comment.