From ce1cc4b0973b74c4132ece60e75cbe3f98299d5e Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 18 Dec 2024 21:12:13 -0800 Subject: [PATCH] [2024][ruby][19.2] --- 2024/ruby/day_19.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/2024/ruby/day_19.rb b/2024/ruby/day_19.rb index ae885b0..7a1d83b 100644 --- a/2024/ruby/day_19.rb +++ b/2024/ruby/day_19.rb @@ -2,8 +2,18 @@ available, desired = DATA.read.split("\n\n") available = available.split(", ") desired = desired.lines(chomp: true) -available = Regexp.new("^(#{available.join(?|)})+$") -pp desired.select { _1 =~ available }.size +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