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/2023/ruby/day_19.rb

21 lines
575 B

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