-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.rb
93 lines (73 loc) · 1.81 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
93
require "set"
file = File.open("input/day07.txt", "r")
lines = file.readlines
class ColorfulBag
def initialize(name)
@name = name
@contains = {}
@contained = {}
end
def putContains(name, amount)
@contains[name] = amount
end
def putContained(name, amount)
@contained[name] = amount
end
def contains
@contains
end
def contained
@contained
end
def countContains(bags)
return 1 + @contains.map {|k, v| v.to_i * bags[k].countContains(bags)}.sum
end
end
def cleanBagName(name)
name = name.strip
if name[name.length-1] == "."
name = name[0..name.length-2]
end
if name[name.length-1] == "s"
name = name[0..name.length-2]
end
return name
end
class Hash
def getOrCreate(key)
# key = bag name
if self.has_key?(key)
return self[key]
else
self[key] = ColorfulBag.new(key)
return self[key]
end
end
end
bags = {}
for line in lines
bagName, bagContains = line.split("contain")
bagName = cleanBagName(bagName)
currentBag = bags.getOrCreate(bagName)
for bagConName in bagContains.split(",").map {|b| b.strip}
bagConName = cleanBagName(bagConName)
if bagConName == "no other bag"
break
end
times, name = bagConName.split(" ", 2)
currentBag.putContains(name, times)
bags.getOrCreate(name).putContained(bagName, times)
end
end
myBagName = "shiny gold bag"
holdableBags = Set[]
checkingBags = [myBagName]
while checkingBags.length > 0
bag = bags[checkingBags.pop]
for k, v in bag.contained
holdableBags.add(k)
checkingBags.push(k)
end
end
puts holdableBags.count # part 1
puts bags[myBagName].countContains(bags)-1 # part 2