You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
advent-of-code/2024/ruby/day_19.rb

29 lines
470 B

available, desired = DATA.read.split("\n\n")
available = available.split(", ")
desired = desired.lines(chomp: true)
available_r = Regexp.new("^(#{available.join(?|)})+$")
pp desired.select { _1 =~ available_r }.size
ways = Hash.new {|h,k|
h[k] = available
.select { k.start_with?(_1) }
.map { k.sub(_1, "") }
.sum { h[_1] }
}
ways[""] = 1
pp desired.sum { ways[_1] }
__END__
r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb