-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.rb
61 lines (50 loc) · 1.33 KB
/
day16.rb
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Aunt
attr_reader :number, :properties
def initialize(number, properties)
@number = number
@properties = properties
end
def score(to_match)
score = 0
to_match.keys.each do |k|
if (@properties[k])
score += 1 if @properties[k] == to_match[k]
end
end
score
end
end
class Processor
def initialize
@aunts = []
end
def parse(line)
#Sue 1: cars: 9, akitas: 3, goldfish: 0
match = line.match(/Sue (\d+):(.+)/)
all, number, properties = match.to_a
props = properties.split(",").map{|p|p.strip.match(/(\w+): (\d+)/)}.map{|m|{m[1] => m[2].to_i}}.reduce(Hash.new, :merge)
@aunts << Aunt.new(number.to_i, props)
end
def find(props_string)
properties = {}
props_string.lines.map{|l|l.strip}.map{|l|l.split(":")}.each{|a|properties[a[0]] = a[1].to_i}
aunt = @aunts.sort_by{|a|a.score(properties)}.reverse.first
puts "aunt #{aunt.number} => score: #{aunt.score(properties)}"
end
end
p = Processor.new
input=File.new("input16.txt").readlines.map{|l|l.strip}
input.each do |l|
p.parse(l)
end
to_match = "children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1"
p.find(to_match)