-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.raku
65 lines (52 loc) · 1.48 KB
/
day3.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
my $contents = "input3.txt".IO.slurp;
my regex mul {'mul('\d+','\d+')'}
my @matches = $contents.comb(&mul);
grammar Calculator {
token TOP { <calc-op> }
proto rule calc-op {*}
rule calc-op:sym<mul> { 'mul('<num>','<num>')'}
token num { \d+ }
}
class Calculations {
method TOP ($/) { make $<calc-op>.made; }
method calc-op:sym<mul> ($/) { make [*] $<num>; }
}
my $sum = 0;
for @matches {
$sum += Calculator.parse($_, actions => Calculations).made;
}
put $sum;
# =============================
# Part 2
# =============================
my $active = True;
grammar BetterCalculator{
token TOP { <command> }
proto token command {*} # Dispatch for commands
token command:sym<activate> { 'do()' }
token command:sym<deactivate> { 'don\'t()' }
token command:sym<mul> { 'mul('<num>','<num>')' }
token num { \d+ }
}
class BetterCalculations {
method TOP ($/) { make $<command>.made; }
method command:sym<mul> ($/) {if $active {make [*] $<num>;} else {make 0;} }
method command:sym<activate> ($/) {$active = True; make 0;}
method command:sym<deactivate> ($/) {$active = False; make 0;}
}
my token do {'do'
['n\'t']?
'()'
}
my regex fil {<mul> | <do>}
my @matches2 = $contents.comb(&fil);
put @matches2;
my $part2 = 0;
my $calc = BetterCalculations.new();
for @matches2 {
my $result = BetterCalculator.parse($_, actions => $calc).made;
if $result.defined {
$part2 += $result;
}
}
put $part2;