blob: d7a69db33334a7011bc6d451c33e15309ce636c4 (
plain)
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
|
rules = {}
gold_producing = {}
lines = 0
File.readlines('input.txt').each do |line|
lines += 1
line.chomp!
bag, contents = line.split 'bags contain'
bag.strip!
rules[bag] = []
contents.split(', ').map do |content|
match = /^(\d+) ([\w\s]+) bags?\.?$/.match(content.strip)
if match
color = match[2]
rules[bag].push color
if color == 'shiny gold'
gold_producing[bag] = true
end
end
end
end
changed = true
while changed
changed = false
rules.each do |bag, productions|
if !gold_producing[bag]
productions.each do |subbag|
if gold_producing[subbag]
gold_producing[bag] = true
changed = true
end
end
end
end
end
puts gold_producing.size
|