Skip to content

Instantly share code, notes, and snippets.

@mscha
Last active December 19, 2024 12:01
Show Gist options
  • Save mscha/686253279ef6c73ca5025af6ea799134 to your computer and use it in GitHub Desktop.
Save mscha/686253279ef6c73ca5025af6ea799134 to your computer and use it in GitHub Desktop.
#!/usr/bin/env raku
use v6.d;
$*OUT.out-buffer = False; # Autoflush
# Advent of Code 2024 day 19 -- https://adventofcode.com/2024/day/19
class TowelArranger
{
has @.towels;
has @.designs;
method count-possible($design)
{
state %cache = '' => 1;
return %cache{$design} //=
@!towels.grep({ ($design.index($_) // -1) == 0 })
.map({ self.count-possible($design.substr(.chars)) })
.sum;
}
method count-possible-designs { @!designs.grep({ self.count-possible($_) }).elems }
method count-possible-ways { @!designs.map({ self.count-possible($_) }).sum }
}
sub MAIN(IO() $inputfile where *.f = 'aoc19.input')
{
my ($towels, $blank, @designs) = $inputfile.lines;
my @towels = $towels.comb(/\w+/);
my $ta = TowelArranger.new(:@towels, :@designs);
say "Part 1: $ta.count-possible-designs() designs are possible.";
say "Part 2: $ta.count-possible-ways() ways to make a design.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment