From 82928ac7c0acdc8db0b632b862a6ed5c7169dca7 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Wed, 16 Dec 2015 08:13:50 -0800 Subject: [PATCH] OOP Day 16 --- day_16.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) 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