-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
9 changed files
with
61 additions
and
26 deletions.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"label": "Advent of TypeScript 2024", | ||
"message": "1/25", | ||
"message": "2/25", | ||
"color": "orange" | ||
} |
Submodule resources
updated
from 1541ba to 00cd52
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
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
import { isReportSafe, parseReports } from './parse.js'; | ||
|
||
export const p1 = (input: string): number => { | ||
return 0; | ||
}; | ||
await task(p1, packageJson.aoc); // 0 ~0ms | ||
export const p1 = (input: string): number => parseReports(input).filter(isReportSafe).length; | ||
|
||
await task(p1, packageJson.aoc); // 299 ~0.58ms |
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
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 |
---|---|---|
@@ -1,7 +1,16 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
import { isReportSafe, parseReports } from './parse.js'; | ||
|
||
export const p2 = (input: string): number => { | ||
return 0; | ||
}; | ||
await task(p2, packageJson.aoc); // 0 ~0ms | ||
export const p2 = (input: string): number => | ||
parseReports(input).filter((report) => { | ||
let safe = isReportSafe(report); | ||
for (let i = 0; i < report.length && !safe; i++) { | ||
const copyOfReport = [...report]; | ||
copyOfReport.splice(i, 1); | ||
safe = isReportSafe(copyOfReport); | ||
} | ||
return safe; | ||
}).length; | ||
|
||
await task(p2, packageJson.aoc); // 364 ~0.90ms |
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 |
---|---|---|
@@ -1,5 +1,36 @@ | ||
export interface Parsed {} | ||
export type Report = number[]; | ||
|
||
export const parse = (input: string): Parsed => { | ||
return undefined; | ||
export const parseReports = (input: string): Report[] => | ||
input.lines().map((line) => line.splitToInt()); | ||
|
||
export const isReportSafe = (report: Report): boolean => { | ||
let monotoneAscending = true; | ||
let monotoneDescending = true; | ||
let safeDiff = true; | ||
let last = undefined; | ||
for (const v of report) { | ||
if (last !== undefined && v <= last) { | ||
monotoneAscending = false; | ||
} | ||
|
||
if (last !== undefined && v >= last) { | ||
monotoneDescending = false; | ||
} | ||
|
||
if (last !== undefined) { | ||
const diff = Math.abs(last - v); | ||
if (diff < 1 || diff > 3) { | ||
safeDiff = false; | ||
break; | ||
} | ||
} | ||
|
||
if (!monotoneAscending && !monotoneDescending) { | ||
break; | ||
} | ||
|
||
last = v; | ||
} | ||
|
||
return (monotoneAscending || monotoneDescending) && safeDiff; | ||
}; |
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