-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.raku
92 lines (75 loc) · 2.51 KB
/
day10.raku
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use v6;
############################################################################################################
# Strategy is to push all opening parens to a stack. When a closing parens is received, we try to
# match it against the last stack element. If it is not a match, then line is deemed corrupted and
# we add it to the error score (part 1); if it is a match, we just pop from stack.
#
# Once all characters from a line are read, we check if line is incomplete (i.e, stack is not empty)
# so we compute the completion score.
############################################################################################################
####################################
# Part 1 utility functions
####################################
sub getErrorScore($c) {
if $c eq ")" { return 3; }
elsif $c eq "]" { return 57; }
elsif $c eq "\}" { return 1197; }
else { return 25137; }
}
sub compareToTail($tail, $c) {
return (( $tail eq "\{" && $c eq "\}" ) ||
( $tail eq "[" && $c eq "]" ) ||
( $tail eq "(" && $c eq ")" ) ||
( $tail eq "<" && $c eq ">" ));
}
sub isClosingChar($c) {
return ( ( $c eq "}" ) ||
( $c eq "]" ) ||
( $c eq ")" ) ||
( $c eq ">" ) );
}
####################################
# Part 2 utility functions
####################################
sub getPoints($c) {
if $c eq "(" { return 1; }
elsif $c eq "[" { return 2; }
elsif $c eq "\{" { return 3; }
else { return 4; }
}
sub computeCompletionScore(@stack) {
my $prize = 0;
for @stack.reverse -> $c {
$prize *= 5;
$prize += getPoints($c);
}
return $prize;
}
my $file = open 'input.txt';
# part 1
my $errorScore = 0;
# part 2
my @completionScores;
for $file.lines -> $line {
next unless $line; # ignore any empty lines
my $error=False;
my @stack;
loop (my $i = 0; $i < $line.chars; $i++) {
my $c = substr($line, $i, 1);
if @stack.elems > 0 && isClosingChar($c) {
if compareToTail(@stack.tail, $c) {
@stack.pop();
} else {
$error=True;
$errorScore += getErrorScore($c);
last;
}
} else {
push(@stack, $c);
}
}
if ! $error { push(@completionScores, computeCompletionScore(@stack)); }
}
say "Day 10, part1: ", $errorScore;
my $index = ((@completionScores.elems - 1) / 2);
say "Day 10, part2: ", @completionScores.sort[$index]