Last active
December 6, 2025 14:03
-
-
Save mscha/1642e06615f6adc8c01069f52fed63e4 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env raku | |
| use v6.d; | |
| $*OUT.out-buffer = False; # Autoflush | |
| # Advent of Code 2025 day 6 -- https://adventofcode.com/2025/day/6 | |
| unit sub MAIN(IO() $inputfile where *.f = 'aoc06.input', Bool :v(:$verbose) = False); | |
| # Apply the operator (+ or *) given as the last element to the remaining terms | |
| sub calculate(@problem is copy) is nodal | |
| { | |
| given @problem.pop { | |
| say '# ', @problem.join(" $_ ") if $verbose; | |
| when '+' { return [+] @problem; } | |
| when '*' { return [*] @problem; } | |
| } | |
| } | |
| # Extract operator from (first) term in input and append as last element | |
| sub extract-operator(@input) is nodal | |
| { | |
| my $oper; | |
| gather for @input -> $term { | |
| if $term ~~ / (\d+) \s* (<[+*]>?) / { | |
| take +$0; | |
| $oper = ~$1 if ~$1; | |
| } | |
| LAST { take $oper } | |
| } | |
| } | |
| # List equivalent of comb: return sublists of consecutive matching elements | |
| sub lcomb(@list, $match) | |
| { | |
| my @ret; | |
| my $row = 0; | |
| for @list -> $elem { | |
| if $elem ~~ $match { | |
| @ret[$row].push($elem); | |
| } | |
| else { | |
| $row++; | |
| } | |
| } | |
| return @ret; | |
| } | |
| my @input1 = [Z] $inputfile.lines».words; | |
| if $verbose { say '> ', $_ for @input1 } | |
| say "Part one: ", @input1».&calculate.sum; | |
| my @input2 = ([Z~] $inputfile.lines».comb).&lcomb(/ \S /)».&extract-operator; | |
| if $verbose { say '> ', $_ for @input2 } | |
| say "Part two: ", @input2».&calculate.sum; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment