Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.16;
- use warnings;
- use List::AllUtils qw(true sum);
- # ASSUME: cards are listed in order
- # Array of number of wins for the N+1th card
- my @card_wins = map {
- my @p = split( /[:|]/ );
- my %win = map { $_ => 1 } split(' ', $p[1]);
- true { exists $win{$_} } split(' ', $p[2])
- } <>;
- # Recursive function: returns number of cards for scratching a card (including self)
- my %memo;
- sub scratch_card {
- my $num = shift;
- my $ret = 1;
- return ($memo{$num}) if (exists $memo{$num});
- $ret += &scratch_card( $num + $_ ) foreach (1 .. $card_wins[$num]);
- return ($memo{$num} = $ret);
- }
- say "Part 1: ", sum map { $_ ? 1 << ($_ - 1) : 0 } @card_wins;
- say "Part 2: ", sum map { &scratch_card( $_ ) } (0 .. $#card_wins);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement