solved part 1
parent
db17f2a1f6
commit
aeb838b832
|
@ -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
|
Loading…
Reference in New Issue