Skip to content
Permalink
a483e4445b
Go to file
 
 
Cannot retrieve contributors at this time
24 lines (20 sloc) 445 Bytes
require 'set'
input = File.read("12.txt")
pipes = {}
input.strip.lines.each { |line|
prog, pipe, *with = line.split.map(&:to_i)
pipes[prog] = with
}
visited = Set.new
p pipes.keys.select { |start|
unless visited.include?(start)
stack = [start]
while prog = stack.pop
visited.add(prog)
pipes[prog].each { |neighbor|
stack.push(neighbor) unless visited.include? neighbor
}
end
true
end
}.size
You can’t perform that action at this time.