-
-
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
10 changed files
with
96 additions
and
14 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": "4/25", | ||
"message": "5/25", | ||
"color": "orange" | ||
} |
Submodule resources
updated
from ab58d4 to 65fbca
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,8 +1,17 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
import { createUpdateComparator, parse } from './parse.js'; | ||
|
||
export const p1 = (_input: string): number => { | ||
return 0; | ||
export const p1 = (input: string): number => { | ||
const p = parse(input); | ||
const comparator = createUpdateComparator(p.rules); | ||
return p.updates | ||
.filter((update) => { | ||
const sortedUpdate = [...update.pages].sort(comparator); | ||
return sortedUpdate.join(',') === update.pages.join(','); | ||
}) | ||
.map((update) => update.pages[Math.floor(update.pages.length / 2)]) | ||
.sum(); | ||
}; | ||
|
||
await task(p1, packageJson.aoc); // 0 ~0.09ms | ||
await task(p1, packageJson.aoc); // 4569 ~24.54ms |
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,8 +1,17 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json' assert { type: 'json' }; | ||
import { createUpdateComparator, parse } from './parse.js'; | ||
|
||
export const p2 = (_input: string): number => { | ||
return 0; | ||
export const p2 = (input: string): number => { | ||
const p = parse(input); | ||
const comparator = createUpdateComparator(p.rules); | ||
return p.updates | ||
.filterMap((update) => { | ||
const sortedUpdate = [...update.pages].sort(comparator); | ||
return sortedUpdate.join(',') !== update.pages.join(',') ? sortedUpdate : undefined; | ||
}) | ||
.map((pages) => pages[Math.floor(pages.length / 2)]) | ||
.sum(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 0 ~0.09ms | ||
await task(p2, packageJson.aoc); // 6456 ~24.37ms |
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 +1,51 @@ | ||
export const parse = (input: string): string[] => input.lines(); | ||
import { DOUBLE_NEWLINE } from '@alexaegis/advent-of-code-lib'; | ||
|
||
export interface Rule { | ||
before: number; | ||
after: number; | ||
} | ||
|
||
export interface Update { | ||
pages: number[]; | ||
} | ||
|
||
export interface Parsed { | ||
rules: Rule[]; | ||
updates: Update[]; | ||
} | ||
|
||
export const createUpdateComparator = (rules: Rule[]) => (a: number, b: number) => { | ||
const mustBeBefore = rules.filter((rule) => rule.before === a).some((rule) => rule.after === b); | ||
const mustBeAfter = rules.filter((rule) => rule.after === a).some((rule) => rule.before === b); | ||
|
||
if (mustBeBefore && mustBeAfter) { | ||
return 0; | ||
} else if (mustBeBefore) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
}; | ||
|
||
export const parse = (input: string): Parsed => { | ||
let [rulesText, pagesText] = input.split(DOUBLE_NEWLINE); | ||
|
||
if (rulesText === undefined || pagesText === undefined) { | ||
throw new Error('Invalid input'); | ||
} | ||
|
||
const rules = rulesText.lines().map((rawRule) => { | ||
const [before, after] = rawRule.splitToInt({ | ||
delimiter: /\|/, | ||
}); | ||
if (before === undefined || after === undefined) { | ||
throw new Error('Invalid number'); | ||
} | ||
return { before, after }; | ||
}); | ||
const updates = pagesText | ||
.lines() | ||
.map((rawRule) => ({ pages: rawRule.splitToInt({ delimiter: /,/ }) }) as Update); | ||
|
||
return { rules, updates }; | ||
}; |
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