mirror of
https://github.com/ershisan99/advent-of-code.git
synced 2025-12-17 04:59:27 +00:00
day 2
This commit is contained in:
76
day-2/day2.ts
Normal file
76
day-2/day2.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
export async function day2(input: string) {
|
||||
const lines = input.split("\n");
|
||||
let count = 0;
|
||||
for (const line of lines) {
|
||||
const levels = line.split(" ").map((v) => Number.parseInt(v, 10));
|
||||
const safe = isSafe(levels);
|
||||
|
||||
if (safe) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
export async function day2part2(input: string) {
|
||||
const lines = input.split("\n");
|
||||
let count = 0;
|
||||
for (const line of lines) {
|
||||
const levels = line.split(" ").map((v) => Number.parseInt(v, 10));
|
||||
const isFullySafe = isSafe(levels);
|
||||
|
||||
if (isFullySafe) {
|
||||
count++;
|
||||
continue;
|
||||
}
|
||||
|
||||
let isDampenedSafe = false;
|
||||
|
||||
for (let i = 0; i < levels.length; i++) {
|
||||
const toTry = levels.toSpliced(i, 1);
|
||||
|
||||
if (isSafe(toTry)) {
|
||||
isDampenedSafe = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isDampenedSafe) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
function isSafe(levels: number[]) {
|
||||
let increasing = null;
|
||||
let safe = true;
|
||||
|
||||
for (let i = 0; i < levels.length - 1; i++) {
|
||||
const current = levels[i];
|
||||
const next = levels[i + 1];
|
||||
const diff = Math.abs(next - current);
|
||||
|
||||
if (diff > 3 || diff < 1) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (i === 0) {
|
||||
increasing = next > current;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (increasing && next <= current) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!increasing && next >= current) {
|
||||
safe = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return safe;
|
||||
}
|
||||
Reference in New Issue
Block a user