-
-
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
9 changed files
with
278 additions
and
21 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 2023", | ||
"message": "3/25", | ||
"message": "4/25", | ||
"color": "orange" | ||
} |
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,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 |
Large diffs are not rendered by default.
Oops, something went wrong.
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,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 |
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,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((card, cardIndex) => { | ||
const pulledWinningNumbers = card.pulledNumbers.filter((pulledNumber) => | ||
card.winningNumbers.includes(pulledNumber), | ||
).length; | ||
for (let i = 1; i <= pulledWinningNumbers; i++) { | ||
const rewardCard = cards[cardIndex + i]; | ||
if (rewardCard) { | ||
card.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 |
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,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, | ||
}; | ||
}; |
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