Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- $/ = '';
- # Read decks:
- my @deck;
- foreach my $p (0 .. 1) {
- $_ = <>;
- $deck[$p]->@* = split( "\n" );
- shift @{$deck[$p]};
- }
- foreach my $p (0 .. 1) {
- print "deck $p: ", join( ',', @{$deck[$p]} ), "\n";
- }
- sub score_deck {
- print "winning deck: ", join( ',', @_ ), "\n";
- my $score = 0;
- my $i = 1;
- while (@_) {
- $score += ($i * pop @_);
- $i++;
- }
- return ($score);
- }
- # Play game:
- while (@{$deck[0]} && @{$deck[1]}) {
- my ($card0, $card1) = (shift @{$deck[0]}, shift @{$deck[1]});
- if ($card0 > $card1) {
- push( @{$deck[0]}, ($card0, $card1) );
- } else {
- push( @{$deck[1]}, ($card1, $card0) );
- }
- }
- # Score:
- print "Score: ", &score_deck( @{$deck[0]} || @{$deck[1]} ), "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement