Last active
December 19, 2024 12:01
-
-
Save mscha/686253279ef6c73ca5025af6ea799134 to your computer and use it in GitHub Desktop.
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 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