-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.rb
92 lines (71 loc) · 1.62 KB
/
day07.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
input_arr = File.open('input.txt').read.split("\n")
bag_objects = {}
bag_names_arr = []
bag_numbers_arr = []
class Bag
attr_accessor :children, :parents, :number
def initialize(children, number)
@parents = []
if children[0] == 'no other'
@children = []
@number = []
else
@children = children
@number = number
end
end
end
input_arr.each do |rule_str|
rule_str.delete! ','
rule_str.delete! '.'
bag_names = rule_str.scan(/(\w* \w*) bag/)
bag_numbers = rule_str.scan(/\d/) # new
arr = []
bag_names.each do |sub_arr|
arr << sub_arr[0]
end
bag_names_arr << arr
arr = [] # new
bag_numbers.each do |sub_arr|
arr << sub_arr[0]
end
bag_numbers_arr << arr
end
bag_names_arr.each_with_index do |bag_list, index|
first_bag = bag_list[0]
bag_objects[first_bag] = Bag.new(bag_list.drop(1), bag_numbers_arr[index])
end
bag_objects.each do |name, object|
object.children.each do |str|
bag_objects[str].parents << name
end
end
ret1 = bag_objects['shiny gold'].parents
flag = false
until flag
flag = true
ret1.each do |bag|
bag_objects[bag].parents.each do |paren|
next if ret1.include?(paren)
ret1 << paren
flag = false
end
end
end
puts ret1.length
ret2 = 0
bag_arr = ['shiny gold']
loop do
break if bag_arr.empty?
temp = []
bag_arr.each do |bag_name|
bag = bag_objects[bag_name]
puts bag.number.class
ret2 += bag.number.map(&:to_i).sum unless bag.number.empty?
bag.children.each_with_index do |child, index|
bag.number[index].to_i.times { temp << child }
end
bag_arr = temp
end
end
puts ret2