blob: 6e00ea34d18f4f48a9b2ab2182dcfb3892b4a6b3 (
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use experimental 'signatures';
use List::Util 'sum';
sub main (@scores) {
my @score_stack = ();
foreach my $score (@scores) {
if ( $score eq "C" ) {
# Clear the previous score
if ( $#score_stack == -1 ) {
die "No scores to remove for 'C' operation\n";
}
pop @score_stack;
}
elsif ( $score eq "D" ) {
# Double the previous score
if ( $#score_stack == -1 ) {
die "No scores to double for 'D' operation\n";
}
push @score_stack, 2 * $score_stack[-1];
}
elsif ( $score eq "+" ) {
# Sum the previous two scores
if ( scalar(@score_stack) < 2 ) {
die "Not enough scores to sum for '+' operation\n";
}
push @score_stack, $score_stack[-1] + $score_stack[-2];
}
elsif ( $score =~ /^-?\d+$/ ) {
# It's a valid integer score
push @score_stack, $score;
}
else {
# We don't know what score this is
die "Invalid score entry: '$score'\n";
}
}
say sum(@score_stack);
}
main(@ARGV);
|