aboutsummaryrefslogtreecommitdiff
path: root/day07.rb
blob: 4793c3e0a421ce8e74840709ba712028bcc70c70 (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
p1 = {}
p2 = {}

IO.foreach("day07") { |line|
  (p1[line[36]] ||= []) << line[5]
  (p2[line[5]] ||= []) << line[36]
  p2[line[36]] ||= []
}

q = p1.values.concat.flatten - p1.keys

fin = []
until q.empty?
  q.sort!
  q.uniq!
  c = q.shift

  print c  unless fin.include? c
  fin << c

  p1.each { |k,v|
    q << k  if (v - fin).empty? && !fin.include?(k)
  }
end
puts # BDHNEGOLQASVWYPXUMZJIKRTFC


t = 0
w = [nil]*5

until p2.empty?
  w.each_with_index { |j, i|
    if j && j[1] == t
      w[i] = nil
      p2.delete(j[0])
    end
  }

  w.each_with_index { |j, i|
    if !j && wn = p2.keys.find { |n|
         p2.none? { |k,v| v.include? n } && w.none? { |j| j && n.include?(j[0]) }
       }
      w[i] = [wn, t + wn.ord - 64 + 60]
    end
  }

  t += 1
end

p t-1

# 1107