Skip to content

Instantly share code, notes, and snippets.

@mscha
Created December 7, 2024 12:57
Show Gist options
  • Select an option

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

Select an option

Save mscha/c1b450dd9d98a8def0aeffbe8b509236 to your computer and use it in GitHub Desktop.
Advent of Code 2024 day 7
#!/usr/bin/env raku
use v6.d;
$*OUT.out-buffer = False; # Autoflush
# Advent of Code 2024 day 7 -- https://adventofcode.com/2024/day/7
sub infix:<∥>(Int $i, Int $j --> Int) { ($i ~ $j).Int }
sub valid-equation($equation)
{
my ($value, @terms) = $equation.comb(/\d+/)».Int;
my @ans = @terms.shift;
for @terms -> $t {
@ans .= map(-> $a { slip($a+$t, $a*$t) }).unique;
}
return any(@ans) == $value ?? $value !! 0;
}
sub valid-equation-v2($equation)
{
my ($value, @terms) = $equation.comb(/\d+/)».Int;
my @ans = @terms.shift;
for @terms -> $t {
@ans .= map(-> $a { slip($a+$t, $a*$t, $a∥$t) }).unique;
}
return any(@ans) == $value ?? $value !! 0;
}
sub MAIN(IO() $inputfile where *.f = 'aoc07.input', Bool :v(:$verbose) = False)
{
my @equations = $inputfile.lines;
say "Part 1: ", @equations».&valid-equation.sum;
say "Part 2: ", @equations».&valid-equation-v2.sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment