-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
lines = (@num|.., _| "\n")* | ||
|
||
num = n:$[0-9]+ { return parseInt(n, 10) } | ||
_ = [ \t]+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { type MainArgs, parseFile } from './lib/utils.ts'; | ||
|
||
type Parsed = number[][]; | ||
|
||
function isSafe(levels: number[]): boolean { | ||
if (levels.length < 2) { | ||
return true; | ||
} | ||
let prev = levels[0]; | ||
const dir = Math.sign(prev - levels[1]); | ||
for (let i = 1; i < levels.length; i++) { | ||
const l = levels[i]; | ||
const diff = prev - l; | ||
if ((diff === 0) || (Math.abs(diff) > 3) || (Math.sign(diff) !== dir)) { | ||
return false; | ||
} | ||
prev = l; | ||
} | ||
return true; | ||
} | ||
|
||
function part1(inp: Parsed): number { | ||
let count = 0; | ||
for (const levels of inp) { | ||
if (isSafe(levels)) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
function part2(inp: Parsed): number { | ||
let count = 0; | ||
for (const levels of inp) { | ||
if (isSafe(levels)) { | ||
count++; | ||
} else { | ||
for (let i=0; i < levels.length; i++) { | ||
if (isSafe(levels.toSpliced(i, 1))) { | ||
count++; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
export default async function main(args: MainArgs): Promise<[number, number]> { | ||
const inp = await parseFile<Parsed>(args); | ||
return [part1(inp), part2(inp)]; | ||
} |
Submodule inputs
updated
from 42749a to bdebd8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default [326, 381]; |