18 lines
571 B
Ruby
Executable File
18 lines
571 B
Ruby
Executable File
#!/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 |