Skip to content

Commit

Permalink
feat: solved 2023 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 4, 2023
1 parent 2f5acdd commit f38e537
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2023.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2023",
"message": "3/25",
"message": "4/25",
"color": "orange"
}
6 changes: 6 additions & 0 deletions resources/2023/04/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
203 changes: 203 additions & 0 deletions resources/2023/04/input.txt

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions solutions/typescript/2023/04/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { describe, expect, it } from 'vitest';
import packageJson from '../package.json';
import { p1 } from './p1.js';

describe('2023 03 p1', () => {
describe('2023 04 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(539_637);
expect(p1(resources.input)).toEqual(20_107);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(4361);
expect(p1(resources.input)).toEqual(13);
});
});
});
21 changes: 16 additions & 5 deletions solutions/typescript/2023/04/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parse } from './parse.js';

export const p1 = (_input: string): number => {
return 0;
};
export const p1 = (input: string): number =>
input
.lines(false)
.map(parse)
.map((card) => {
let score = 0;
for (const pulledNumber of card.pulledNumbers) {
if (card.winningNumbers.includes(pulledNumber)) {
score = score * 2 || 1;
}
}
return score;
})
.sum();

await task(p1, packageJson.aoc); // 539637 ~1.39ms
await task(p1, packageJson.aoc); // 20107 ~4.36ms
6 changes: 3 additions & 3 deletions solutions/typescript/2023/04/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import { describe, expect, it } from 'vitest';
import packageJson from '../package.json';
import { p2 } from './p2.js';

describe('2023 03 p2', () => {
describe('2023 04 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(82_818_007);
expect(p2(input)).toEqual(8_172_507);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(467_835);
expect(p2(input)).toEqual(30);
});
});
});
27 changes: 23 additions & 4 deletions solutions/typescript/2023/04/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { parse } from './parse.js';

export const p2 = (_input: string): number => {
return 1;
export const p2 = (input: string): number => {
const cards = input.lines(false).map(parse);
cards.forEach((game, gameIndex) => {
const pulledWinningNumbers = game.pulledNumbers.filter((pulledNumber) =>
game.winningNumbers.includes(pulledNumber),
).length;
for (let i = 1; i <= pulledWinningNumbers; i++) {
const rewardCard = cards[gameIndex + i];
if (rewardCard) {
game.rewardCards.push(rewardCard);
}
}
});

for (const card of cards) {
for (const rewardCard of card.rewardCards) {
rewardCard.count = rewardCard.count + card.count;
}
}

return cards.map((card) => card.count).sum();
};

await task(p2, packageJson.aoc); // 82818007 ~3.89ms
await task(p2, packageJson.aoc); // 8172507 ~4.57ms
26 changes: 22 additions & 4 deletions solutions/typescript/2023/04/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
export interface Game {
export interface Card {
id: number;
winningNumbers: number[];
pulledNumbers: number[];
rewardCards: Card[];
count: number;
}

export const parse = (line: string): Game => {
console.log(line);
export const parse = (line: string): Card => {
const [rawCardId, numbers] = line.splitIntoStringPair(': ');
const [, idString] = rawCardId.splitIntoStringPair(/ +/);
const [rawWinningNumbers, rawPulledNumbers] = numbers.splitIntoStringPair(' | ');
const winningNumbers = rawWinningNumbers
.split(/ +/g)
.filter((l) => !!l)
.map((n) => Number.parseInt(n, 10));
const pulledNumbers = rawPulledNumbers
.split(/ +/g)
.filter((l) => !!l)
.map((n) => Number.parseInt(n, 10));
return {
id: 1,
winningNumbers,
id: Number.parseInt(idString, 10),
pulledNumbers,
rewardCards: [],
count: 1,
};
};
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| [Day 1](/solutions/typescript/2023/01/) | [0.19ms](/solutions/typescript/2023/01/src/p1.ts) | [1.38ms](/solutions/typescript/2023/01/src/p2.ts) |
| [Day 2](/solutions/typescript/2023/02/) | [0.22ms](/solutions/typescript/2023/02/src/p1.ts) | [0.25ms](/solutions/typescript/2023/02/src/p2.ts) |
| [Day 3](/solutions/typescript/2023/03/) | [1.39ms](/solutions/typescript/2023/03/src/p1.ts) | [3.89ms](/solutions/typescript/2023/03/src/p2.ts) |
| [Day 4](/solutions/typescript/2023/04/) | [?ms](/solutions/typescript/2023/04/src/p1.ts) | [?ms](/solutions/typescript/2023/04/src/p2.ts) |
| [Day 4](/solutions/typescript/2023/04/) | [4.36ms](/solutions/typescript/2023/04/src/p1.ts) | [4.57ms](/solutions/typescript/2023/04/src/p2.ts) |
| Day 5 | - | - |
| Day 6 | - | - |
| Day 7 | - | - |
Expand Down

0 comments on commit f38e537

Please sign in to comment.