Skip to content

Commit

Permalink
Year 2023, Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 4, 2023
1 parent f213ebb commit 8fdf997
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 0 deletions.
24 changes: 24 additions & 0 deletions clients/typescript/solutions/S2304.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ISolution from "./ISolution.ts";

export default class S2303 implements ISolution {
firstPart(input: string): string | number {
return input.split('\n').map(line => {
const numbers = line.split(': ')[1].split(' | ').map(s => s.split(' ').filter(s => s !== '').map(Number));
return numbers[1].filter(n => numbers[0].includes(n)).length;
}).map(n => n > 0 ? 2**(n-1) : 0).reduce((a, b) => a + b, 0);
}
secondPart(input: string): string | number {
const winners = input.split('\n').map(line => {
const numbers = line.split(': ')[1].split(' | ').map(s => s.split(' ').filter(s => s !== '').map(Number));
return numbers[1].filter(n => numbers[0].includes(n)).length;
});
const cards: number[] = winners.map(_ => 1);
winners.forEach((winner, index) => {
for (let i = 0; i < winner; i++) {
cards[index+i+1] += cards[index];
}
});
return cards.reduce((a, b) => a + b, 0);
}

}
Loading

0 comments on commit 8fdf997

Please sign in to comment.