From 84f71ef018991e4edf4d1d05c9c5928ab3840868 Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Sun, 1 Dec 2024 10:06:08 +0100 Subject: [PATCH] feat: 2024 01 --- .github/badges/typescript/2024.json | 2 +- resources | 2 +- solutions/typescript/2024/01/src/p1.ts | 14 ++++-- solutions/typescript/2024/01/src/p2.ts | 12 ++++- solutions/typescript/2024/01/src/parse.ts | 19 ++++++++ solutions/typescript/readme.md | 54 +++++++++++------------ 6 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 solutions/typescript/2024/01/src/parse.ts diff --git a/.github/badges/typescript/2024.json b/.github/badges/typescript/2024.json index 1cd630c7..a596722e 100644 --- a/.github/badges/typescript/2024.json +++ b/.github/badges/typescript/2024.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, "label": "Advent of TypeScript 2024", - "message": "0/25", + "message": "1/25", "color": "orange" } diff --git a/resources b/resources index 967c011b..1541babb 160000 --- a/resources +++ b/resources @@ -1 +1 @@ -Subproject commit 967c011b6b5531f08afaed1717c7e4fc9998db98 +Subproject commit 1541babb6aa78ad527c5dab2b1492176e29d4c9c diff --git a/solutions/typescript/2024/01/src/p1.ts b/solutions/typescript/2024/01/src/p1.ts index 2dd64deb..efe9f248 100644 --- a/solutions/typescript/2024/01/src/p1.ts +++ b/solutions/typescript/2024/01/src/p1.ts @@ -1,7 +1,15 @@ -import { task } from '@alexaegis/advent-of-code-lib'; +import { ascending, task, zip } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json' assert { type: 'json' }; +import { parse } from './parse.js'; export const p1 = (input: string): number => { - return 0; + const { left, right } = parse(input); + + left.sort(ascending); + right.sort(ascending); + + return zip(left, right) + .map(([l, r]) => Math.abs(l - r)) + .sum(); }; -await task(p1, packageJson.aoc); // 0 ~0ms +await task(p1, packageJson.aoc); // 1722302 ~16.04ms diff --git a/solutions/typescript/2024/01/src/p2.ts b/solutions/typescript/2024/01/src/p2.ts index 9e79a576..da7886ba 100644 --- a/solutions/typescript/2024/01/src/p2.ts +++ b/solutions/typescript/2024/01/src/p2.ts @@ -1,8 +1,16 @@ import { task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json' assert { type: 'json' }; +import { parse } from './parse.js'; export const p2 = (input: string): number => { - return 0; + const { left, right } = parse(input); + + return left + .map((l) => { + const rigthAppearance = right.filter((r) => r === l).length; + return l * rigthAppearance; + }) + .sum(); }; -await task(p2, packageJson.aoc); // 0 ~0ms +await task(p2, packageJson.aoc); // 20373490 ~18.95ms diff --git a/solutions/typescript/2024/01/src/parse.ts b/solutions/typescript/2024/01/src/parse.ts new file mode 100644 index 00000000..2d966e95 --- /dev/null +++ b/solutions/typescript/2024/01/src/parse.ts @@ -0,0 +1,19 @@ +import { WHITESPACE } from '@alexaegis/advent-of-code-lib'; + +export interface SeparatedInput { + left: number[]; + right: number[]; +} + +export const parse = (input: string): SeparatedInput => + input + .lines() + .map((line) => line.splitIntoStringPair(WHITESPACE)) + .reduce( + (acc, [left, right]) => { + acc.left.push(parseInt(left, 10)); + acc.right.push(parseInt(right, 10)); + return acc; + }, + { left: [], right: [] } as SeparatedInput, + ); diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 42fb58fb..285056b6 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -6,33 +6,33 @@ -| Day | Part One | Part Two | -| --------------------------------------- | ---------------------------------------------- | ---------------------------------------------- | -| [Day 1](/solutions/typescript/2024/01/) | [0ms](/solutions/typescript/2024/01/src/p1.ts) | [0ms](/solutions/typescript/2024/01/src/p2.ts) | -| Day 2 | - | - | -| Day 3 | - | - | -| Day 4 | - | - | -| Day 5 | - | - | -| Day 6 | - | - | -| Day 7 | - | - | -| Day 8 | - | - | -| Day 9 | - | - | -| Day 10 | - | - | -| Day 11 | - | - | -| Day 12 | - | - | -| Day 13 | - | - | -| Day 14 | - | - | -| Day 15 | - | - | -| Day 16 | - | - | -| Day 17 | - | - | -| Day 18 | - | - | -| Day 19 | - | - | -| Day 20 | - | - | -| Day 21 | - | - | -| Day 22 | - | - | -| Day 23 | - | - | -| Day 24 | - | - | -| Day 25 | - | - | +| Day | Part One | Part Two | +| --------------------------------------- | -------------------------------------------------- | -------------------------------------------------- | +| [Day 1](/solutions/typescript/2024/01/) | [16.04ms](/solutions/typescript/2024/01/src/p1.ts) | [18.95ms](/solutions/typescript/2024/01/src/p2.ts) | +| Day 2 | - | - | +| Day 3 | - | - | +| Day 4 | - | - | +| Day 5 | - | - | +| Day 6 | - | - | +| Day 7 | - | - | +| Day 8 | - | - | +| Day 9 | - | - | +| Day 10 | - | - | +| Day 11 | - | - | +| Day 12 | - | - | +| Day 13 | - | - | +| Day 14 | - | - | +| Day 15 | - | - | +| Day 16 | - | - | +| Day 17 | - | - | +| Day 18 | - | - | +| Day 19 | - | - | +| Day 20 | - | - | +| Day 21 | - | - | +| Day 22 | - | - | +| Day 23 | - | - | +| Day 24 | - | - | +| Day 25 | - | - |