-
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.
Merge pull request #16 from hildjj/day5
Day 5
- Loading branch information
Showing
4 changed files
with
48 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,10 @@ | ||
manual = @order "\n" @pages | ||
|
||
order = (@num "|" @num "\n")* | ||
|
||
pages = (@page "\n")+ | ||
|
||
page = num|1.., ","| | ||
|
||
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,36 @@ | ||
import { type MainArgs, parseFile } from './lib/utils.ts'; | ||
|
||
type Parsed = [[number, number][], number[][]]; | ||
|
||
function part1(inp: number[][][]): number { | ||
return inp | ||
.filter(([before, after]) => before.join(',') === after.join(',')) | ||
.reduce((t, [before]) => t + before[Math.floor(before.length / 2)], 0); | ||
} | ||
|
||
function part2(inp: number[][][]): number { | ||
return inp | ||
.filter(([before, after]) => before.join(',') !== after.join(',')) | ||
.reduce((t, [_, after]) => t + after[Math.floor(after.length / 2)], 0); | ||
} | ||
|
||
export default async function main(args: MainArgs): Promise<[number, number]> { | ||
const inp = await parseFile<Parsed>(args); | ||
const order = new Set<string>(); | ||
for (const [x, y] of inp[0]) { | ||
order.add(`${x},${y}`); | ||
} | ||
const beforeAndAfter = inp[1].map((pages) => { | ||
const sorted = [...pages].sort((a, b) => { | ||
if (order.has(`${a},${b}`)) { | ||
return -1; | ||
} | ||
if (order.has(`${b},${a}`)) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
return [pages, sorted]; | ||
}); | ||
return [part1(beforeAndAfter), part2(beforeAndAfter)]; | ||
} |
Submodule inputs
updated
from f9def5 to 545dfb
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 [4662, 5900]; |