This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
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.
* add deno tasks * update gitignore * update workflow file * refactor * use camelCase * add comment * refactor * direct return * use camelCase * fix ci lcov
- Loading branch information
Showing
17 changed files
with
171 additions
and
174 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
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,3 +1,4 @@ | ||
deno.lock | ||
**/input.txt | ||
.vscode | ||
cov_profile |
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,49 +1,47 @@ | ||
export function parse(data: string): number[][] { | ||
const parsed = data.trim().split("\n").map((x) => | ||
x.split(" ").map((x) => parseInt(x)) | ||
// 3 spaces between the numbers | ||
x.split(" ").map((x) => parseInt(x)) | ||
); | ||
return parsed | ||
// Three spaces are used to separate the numbers | ||
.map((x) => [x[0], x[3]]); | ||
.map((x) => [x[0], x[1]]); | ||
} | ||
|
||
export function solve1(data: number[][]): number { | ||
const left_nums = data.map((x) => x[0]); | ||
const right_nums = data.map((x) => x[1]); | ||
const leftNums = data.map((x) => x[0]); | ||
const rightNums = data.map((x) => x[1]); | ||
|
||
const sorted_left_nums = left_nums.sort((a, b) => a - b); | ||
const sorted_right_nums = right_nums.sort((a, b) => a - b); | ||
const sortedLeftNums = leftNums.sort((a, b) => a - b); | ||
const sortedRightNums = rightNums.sort((a, b) => a - b); | ||
|
||
return Array.from({ length: data.length }, (_, i) => i) | ||
.reduce( | ||
(acc, i) => acc + Math.abs(sorted_left_nums[i] - sorted_right_nums[i]), | ||
(acc, i) => acc + Math.abs(sortedLeftNums[i] - sortedRightNums[i]), | ||
0, | ||
); | ||
} | ||
|
||
export function solve2(data: number[][]): number { | ||
const left_nums = data.map((x) => x[0]); | ||
const right_nums = data.map((x) => x[1]); | ||
const leftNums = data.map((x) => x[0]); | ||
const rightNums = data.map((x) => x[1]); | ||
|
||
const right_count = right_nums.reduce<Record<number, number>>( | ||
const rightCount = rightNums.reduce<Record<number, number>>( | ||
(acc, x) => { | ||
acc[x] = (acc[x] || 0) + 1; | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
|
||
return left_nums.reduce( | ||
(acc, x) => { | ||
return acc + (x * (right_count[x] || 0)); | ||
}, | ||
return leftNums.reduce( | ||
(acc, x) => acc + (x * (rightCount[x] || 0)), | ||
0, | ||
); | ||
} | ||
|
||
if (import.meta.main) { | ||
const data_path = new URL("input.txt", import.meta.url).pathname; | ||
const data = parse(await Deno.readTextFile(data_path)); | ||
const dataPath = new URL("input.txt", import.meta.url).pathname; | ||
const data = parse(await Deno.readTextFile(dataPath)); | ||
console.log(solve1(data)); | ||
console.log(solve2(data)); | ||
} |
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
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
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
Oops, something went wrong.