-
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.
feat(js): add solutions for 2023 day 4
- Loading branch information
1 parent
4ff47b7
commit 78575b2
Showing
3 changed files
with
258 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,41 @@ | ||
/* eslint-disable no-cond-assign, no-loop-func */ | ||
|
||
import { cast, solution } from '../../utils'; | ||
|
||
const parse = (input) => ( | ||
input.trim().split('\n').map((line) => { | ||
const [first, second] = line.split('|'); | ||
const [id, ...winningNrs] = first.match(/\d+/g).map(cast); | ||
const nrs = second.match(/\d+/g).map(cast); | ||
return { id, winningNrs, nrs }; | ||
}) | ||
); | ||
|
||
export const partOne = solution((input) => { | ||
const cards = parse(input); | ||
let score = 0; | ||
for (const card of cards) { | ||
const winners = card.nrs.filter((have) => card.winningNrs.includes(have)); | ||
if (winners.length) { | ||
score += 2 ** (winners.length - 1); | ||
} | ||
} | ||
return score; | ||
}); | ||
|
||
export const partTwo = solution((input) => { | ||
const cards = parse(input); | ||
|
||
const pile = [...cards]; | ||
let card; | ||
let amount = 0; | ||
while (card = pile.pop()) { | ||
++amount; | ||
const winners = card.nrs.filter((have) => card.winningNrs.includes(have)); | ||
if (winners.length) { | ||
const rewards = cards.slice(card.id, card.id + winners.length); | ||
pile.push(...rewards); | ||
} | ||
} | ||
return amount; | ||
}); |
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,13 @@ | ||
part-one: | ||
- input: &example1 | | ||
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 | ||
answer: 13 | ||
|
||
part-two: | ||
- input: *example1 | ||
answer: 30 |
Oops, something went wrong.