Skip to content

Instantly share code, notes, and snippets.

@mscha
Last active December 6, 2025 14:03
Show Gist options
  • Select an option

  • Save mscha/1642e06615f6adc8c01069f52fed63e4 to your computer and use it in GitHub Desktop.

Select an option

Save mscha/1642e06615f6adc8c01069f52fed63e4 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 6
#!/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