use strict; use warnings; my %pairs = ('(', ')', '[', ']', '{', '}', '<', '>'); my %scores = (')' => 3, ']' => 57, '}' => 1197, '>' => 25137); my $score; while (<>) { chomp; my @stack; for (split //) { if (index('([{<', $_) != -1) { unshift @stack, $pairs{$_}; } elsif ($_ eq $stack[0]) { shift @stack; } else { $score += $scores{$_}; last; } } } print "$score\n";