diff --git a/day_16.rb b/day_16.rb index 604d4f9..75b0b64 100644 --- a/day_16.rb +++ b/day_16.rb @@ -1,8 +1,33 @@ +require "letters" + +class Sue + MATCHERS = Hash.new("==") + %w[ cats trees ].each do |key| + MATCHERS[key] = ?> + end + %w[pomeranians goldfish ].each do |key| + MATCHERS[key] = ?< + end + + attr_reader *%i[ properties ] + + def initialize(properties) + @properties = properties + end + + def match?(facts) + facts.select {|k,_| properties.keys.include?(k) } + .all? {|k,v| properties[k].send(MATCHERS[k], v) } + end +end + regex = /Sue (\d+): (.*)/ sues = Hash[DATA.read.scan(regex).map { |attrs| - [attrs[0], Hash[attrs[1].split(", ").map {|i| i.split(": ") }]] + [attrs[0].to_i, Sue.new(Hash[attrs[1].split(", ") + .map {|i| i.split(": ") } + .map {|k,v| [k,v.to_i] }])] }] -facts = Hash[< v } && - facts_lt.select {|k,_| data.keys.include?(k) }.all? {|k,v| data[k] < v } -} +puts sues.select {|i,sue| sue.match?(facts) } __END__ Sue 1: goldfish: 9, cars: 0, samoyeds: 9