Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 2, 2024
1 parent 9c99d4b commit ebac894
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions src/2024/day02.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
function isSafe(line) {
const result = line.map((num, i) => i > 0 && num - line[i - 1]).slice(1);

This comment has been minimized.

Copy link
@LeonFedotov

LeonFedotov Dec 2, 2024

cool stuff :)
checkout mine day2

return (
result.every(num => num >= 1 && num <= 3) ||
result.every(num => num <= -1 && num >= -3)
);
}

export function part1(input) {
const lines = input.split("\n").map(line => line.split(" ").map(Number));
const safe = lines.filter(line => {
const result = line
.map((num, i) => (i === 0 ? 0 : num - line[i - 1]))
.slice(1);
return (
result.every(num => num >= 1 && num <= 3) ||
result.every(num => num <= -1 && num >= -3)
);
});
return safe.length;
return lines.filter(isSafe).length;
}

export function part2(input) {
const lines = input.split("\n").map(line => line.split(" ").map(Number));
const safe = lines.filter(line => {
for (let i = 0; i < line.length; i++) {
const newLine = line.slice();
newLine.splice(i, 1);
const result = newLine
.map((num, i) => (i === 0 ? 0 : num - newLine[i - 1]))
.slice(1);
if (
result.every(num => num >= 1 && num <= 3) ||
result.every(num => num <= -1 && num >= -3)
)
return true;
}
});
return safe.length;
return lines.filter(line => {
return line.map((x, i) => line.toSpliced(i, 1)).find(isSafe);
}).length;
}

0 comments on commit ebac894

Please sign in to comment.