aboutsummaryrefslogtreecommitdiff
path: root/challenge-336/deadmarshal/perl/ch-2.pl
blob: f835300aa3992f5498377b0d39b9dcb1bde537e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub final_score{
  my @stk;
  for(@{$_[0]}) {
    if(/\d+/) {
      push @stk,$_
    } elsif($_ eq 'C') {
      pop @stk
    } elsif($_ eq 'D') {
      push @stk,$stk[-1] * 2
    } else {
      push @stk,$stk[-2] + $stk[-1]
    }
  }
  sum0 @stk
}

printf "%d\n",final_score(['5','2','C','D','+']);
printf "%d\n",final_score(['5','-2','4','C','D','9','+','+']);
printf "%d\n",final_score(['7','D','D','C','+','3']);
printf "%d\n",final_score(['-5','-10','+','D','C','+']);
printf "%d\n",final_score(['3','6','+','D','C','8','+','D',
			   '-2','C','+']);