use strict; use warnings; my %pairs = ('(', ')', '[', ']', '{', '}', '<', '>'); my %scores = (')' => 3, ']' => 57, '}' => 1197, '>' => 25137); my ($score, @scores2); LINE: while (<>) { chomp; my @stack; for (split //) { if (index('([{<', $_) != -1) { unshift @stack, $pairs{$_}; } elsif ($_ eq $stack[0]) { shift @stack; } else { $score += $scores{$_}; next LINE; } } next unless @stack; my $score2 = 0; $score2 = $score2 * 5 + 1 + index(')]}>', $_) for @stack; push @scores2, $score2; } print "$score\n"; @scores2 = sort { $a <=> $b } @scores2; print $scores2[@scores2 / 2], "\n";