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 }}