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";
e 2021-01-20Use mtags in source-filterJune McEnroe 2021-01-19Add mtags to generate tags for make and mdocJune McEnroe 2021-01-19Map tags to IDs using only [[:alnum:]-._]June McEnroe 2021-01-19Don't use a pager if reading standard inputJune McEnroe 2021-01-19Support BSD make syntax and match *.amJune McEnroe These don't really go together, but... 2021-01-19Match tab following escaped newline in make assignmentsJune McEnroe Otherwise it ends up going into Shell state. 2021-01-18Allow matching lexers using first input lineJune McEnroe