aboutsummaryrefslogtreecommitdiff
path: root/day07/part1
blob: bd927589c10a12f64f5b5dd45ee29d123778b2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env ruby

input = $stdin.readlines.map do |i|
  i =~ /Step ([A-Z]) must be finished before step ([A-Z]) can begin./
  [$2, $1]
end

steps = input.flatten.uniq.sort
prereqs = {}

steps.each do |s|
  prereqs[s] = []
end

input.each do |i|
  prereqs[i.first] << i.last
end

order = []

until prereqs.empty? do
  next_step = prereqs.select { |k,v| v.empty? }.keys.sort.first
  order << next_step
  prereqs.delete(next_step)
  prereqs.each_key do |k|
    prereqs[k].delete(next_step)
  end
end

puts order.join