1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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].to_i, Sue.new(Hash[attrs[1].split(", ")
.map {|i| i.split(": ") }
.map {|k,v| [k,v.to_i] }])]
}]
facts = Hash[<<FACTS.split("\n").map {|i| i.split(": ") }.map {|k,v| [k,v.to_i] }]
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
FACTS
puts sues.select {|i,sue| sue.match?(facts) }
__END__