summary refs log tree commit diff homepage
path: root/2021/day10.pl
blob: e35db27c6a3f0b7202d1cba3dbe556282282a2ef (plain) (blame)
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
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";