Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Last active December 9, 2018 22:41
Show Gist options
  • Save omarqureshi/6e72977bf3bea80373807a95420dc39e to your computer and use it in GitHub Desktop.
Save omarqureshi/6e72977bf3bea80373807a95420dc39e to your computer and use it in GitHub Desktop.
steps = Hash.new([])
File.open("./input.txt") do |file|
file.each_line do |line|
line =~ /Step ([A-Z]) must be finished before step ([A-Z])/
step = $2
prereq = $1
steps[prereq] = [] unless steps.has_key?(prereq)
steps[step] += [prereq]
end
puts steps.inspect
deps = []
while steps.any?
next_steps = {}
deps += steps.select { |k,v| v == []}.to_h.sort_by { |k,v| k }.to_h.keys
steps = steps.reject { |k, v| deps.include?(k) }
steps = steps.each do |k, v|
v = v - deps
next_steps[k] = v
end
steps = next_steps
end
puts deps.join("")
end
{"L"=>[], "A"=>["L"], "P"=>["L"], "F"=>["P"], "V"=>[], "U"=>["V", "T", "M", "K", "C", "B"], "S"=>["F", "B", "Z", "C", "O", "X"], "J"=>["A", "N", "S", "I", "D", "U", "O", "H", "B", "M"], "R"=>[], "K"=>["R", "H", "T", "L"], "Z"=>["R"], "T"=>["Z", "R"], "G"=>["R"], "W"=>["G", "K"], "H"=>["G"], "B"=>["K", "A", "C", "W", "L", "N", "T"], "C"=>["F"], "Y"=>["C", "U", "Q", "J", "I", "O", "D", "M", "S", "B", "A", "G", "P"], "N"=>["W", "H", "C", "P"], "E"=>["K", "F", "P", "W", "H", "C"], "M"=>["E", "G", "W", "Z", "X"], "O"=>["V", "Z", "G"], "D"=>["O", "X", "Z", "I", "N", "S", "U", "F", "G"], "X"=>["T", "N", "W"], "Q"=>["M", "B", "C", "S", "D", "U", "G", "I"], "I"=>["B", "F", "R", "G", "P", "X"]}
LRVAGPZFHOTCKWENBXIMSUDJQY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment