solved part 1

master
Jeff Yates 2020-12-08 15:49:29 -05:00
parent db17f2a1f6
commit aeb838b832
1 changed files with 18 additions and 0 deletions

18
day07/solution01.rb Executable file
View File

@ -0,0 +1,18 @@
#!/bin/env ruby
def can_hold? (bags, color)
parents = bags.map { |bag| bag[1..].
map { |child| bag[0] if child.include?(color)}
}.flatten.compact
if parents.any?
parents += parents.map { |col| can_hold?(bags, col) }
end
parents.flatten.uniq
end
bags = File.readlines('./input').map { |line| line.delete('.').strip.split('contain').
map { |item| item.strip.split(',')}.flatten
}
bags.map { |line| line.map { |str| str.slice!('bags') }}
bags.map { |line| line.map { |str| str.slice!('bag') }}
p can_hold?(bags, 'shiny gold').length