Skip to content

Commit

Permalink
feat(js): add solutions for 2023 day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Dec 4, 2023
1 parent 4ff47b7 commit 78575b2
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 0 deletions.
41 changes: 41 additions & 0 deletions js/src/2023/04/index.js
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;
});
13 changes: 13 additions & 0 deletions puzzles/2023/04/examples.yaml
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
Loading

0 comments on commit 78575b2

Please sign in to comment.