From 2364abf76af9ac9c76d59b45fe42e7c277967918 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 19 Dec 2023 09:32:02 -0800 Subject: [PATCH] [2023][ruby][19.1] --- 2023/ruby/day_19.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 2023/ruby/day_19.rb diff --git a/2023/ruby/day_19.rb b/2023/ruby/day_19.rb new file mode 100644 index 0000000..69ccba7 --- /dev/null +++ b/2023/ruby/day_19.rb @@ -0,0 +1,20 @@ +workflows, parts = ARGF.read.split("\n\n") + +Part = Data.define(:x, :m, :a, :s) + +parts = parts.lines(chomp: true).map { Part.new(**eval(_1.gsub(?=, ?:))) } + +workflows = workflows.scan(/(\w+)\{((?~}))\}/).to_h {|name, rules| + rules = rules.split(?,).map {|rule| + rule.include?(?:) ? rule.split(?:) : ["true", rule] + } + [name, rules] +} + +pp parts.select {|part| + workflow = "in" + until %w[A R].include?(workflow) do + workflow = workflows.fetch(workflow).find {|cond,_| part.instance_eval(cond) }.last + end + workflow == ?A +}.sum { _1.instance_eval { x + m + a + s }}