2023 AoC Day 4 – Scratchcards

This is a solution to Advent of Code 2023 day 4, written in Raku.

https://adventofcode.com/2023/day/4

Part One

As far as the Elf has been able to figure out, you have to figure out which of the numbers you have appear in the list of winning numbers. The first match makes the card worth one point and each match after the first doubles the point value of that card.

How many points are they worth in total?

my $total = 0;
for '4-input.txt'.IO.lines>>.split(': ') -> ($card, $numbers) {
    my ($ours, $winning) = $numbers.split(' | ')>>.comb(/\d+/)>>.Set;
    my $wins = ($ours (&) $winning).elems;
    if $wins {
        $total += 2 ** ($wins - 1);
    }
}
say "Scratchcards are worth {$total} points";
Scratchcards are worth 24733 points

Part Two

There's no such thing as "points". Instead, scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have …

Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, how many total scratchcards do you end up with?

my @many;
my $pos = 0;
for '4-input.txt'.IO.lines>>.split(': ') -> ($card, $numbers) {
    @many[$pos]++;
    my ($ours, $winning) = $numbers.split(' | ')>>.comb(/\d+/)>>.Set;
    my $wins = ($ours (&) $winning).elems;

    if $wins {
        my $n = @many[$pos];
        @many[$_] += $n for $pos ^.. $pos + $wins;
    }
    $pos++;
}
say "End up with {[+] @many} scratchcards";
End up with 5422730 scratchcards
raku