-
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.
- Loading branch information
1 parent
f213ebb
commit 8fdf997
Showing
2 changed files
with
230 additions
and
0 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 |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
Oops, something went wrong.