m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/07/a.rb
diff options
context:
space:
mode:
Diffstat (limited to '07/a.rb')
-rw-r--r--07/a.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/07/a.rb b/07/a.rb
new file mode 100644
index 0000000..d7a69db
--- /dev/null
+++ b/07/a.rb
@@ -0,0 +1,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