aboutsummaryrefslogtreecommitdiff
path: root/day07.rb
diff options
context:
space:
mode:
Diffstat (limited to 'day07.rb')
-rw-r--r--day07.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/day07.rb b/day07.rb
new file mode 100644
index 0000000..4793c3e
--- /dev/null
+++ b/day07.rb
@@ -0,0 +1,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