Skip to content

Instantly share code, notes, and snippets.

@KrzaQ
Created December 7, 2018 07:04
Show Gist options
  • Save KrzaQ/c7779f94249b3193afa30afb4a392c61 to your computer and use it in GitHub Desktop.
Save KrzaQ/c7779f94249b3193afa30afb4a392c61 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
DATA = File
.read('data.txt')
.scan(/Step (.) must be finished before step (.) can begin/)
def produce_order g
ret = ''
chars = DATA.flatten.uniq
todo = chars.select{ |c| g.fetch(c, []).size == 0 }.sort
loop do
t = todo.first
todo = todo[1..-1]
ret += t unless ret.include? t
todo += chars.select do |c|
a = g.fetch(c, [t])
a.size == 1 and a.first == t
# v.map{ |el| p el; g.fetch(el, []).size == 0 }.all?
end
.reject{ |c| ret.include? c }
.reject{ |c| t == c }
todo.sort!
todo.uniq!
for k, v in g
v.delete t
g[k] = v
end
chars = chars
.reject{ |c| todo.include? c }
.reject{ |c| ret.include? c }
g.delete t
break if todo.size == 0
end
ret
end
g = DATA
.map(&:reverse)
.each_with_object({}){ |a, m| tmp = m.fetch(a.first, []); tmp << a.last; m[a.first] = tmp.sort; m }
.to_h
# puts produce_order(g)
def get_time g
ret = ''
chars = DATA.flatten.uniq
todo = chars.select{ |c| g.fetch(c, []).size == 0 }.sort
secs = 0
# p g
# loop do
workers = (0...5).map{ [0, nil] }
# p ('='*80)
# p todo
# p ret
# t = todo.first
# todo = todo[1..-1]
p workers
# for t in todo
# ret += t unless ret.include? t
# g.delete t
# for k, v in g
# v.delete t
# g[k] = v
# end
# end
# p g
while todo.size > 0 or workers.map(&:first).sum > 0 or g.size > 0
p [workers, todo, secs]
secs += 1
# p g
# exit if secs > 30
if g.size > 0
todo += chars.select do |c|
a = g.fetch(c, [])
a.size == 0
# v.map{ |el| p el; g.fetch(el, []).size == 0 }.all?
end
.reject{ |c| ret.include? c }
.reject{ |c| workers.map(&:last).include? c }
# .reject{ |c| t == c }
todo.sort!
todo.uniq!
end
for i in (0...workers.size)
# x = workers[i]
# p x
if workers[i][0] == 0 and todo.size > 0
p todo
t = todo.first
todo = todo[1..-1]
workers[i][0] = t.ord - 'A'.ord + 61
workers[i][1] = t
end
if workers[i][0] > 0
# p workers[i]
workers[i][0] -= 1
if workers[i][0] == 0
g.delete workers[i][1]
ret += workers[i][1]
for k, v in g
# puts 'deleting %s' % workers[i][1]
v.delete workers[i][1]
g[k] = v
end
workers[i][1] = nil
end
end
end
end
# p g if g.size > 0
# p ['chars', chars]
# todo += chars.select do |c|
# a = g.fetch(c, [])
# a.size == 0
# # v.map{ |el| p el; g.fetch(el, []).size == 0 }.all?
# end
# .reject{ |c| ret.include? c }
# # .reject{ |c| t == c }
# todo.sort!
# todo.uniq!
# # p g if g.size > 0
# chars = chars
# .reject{ |c| todo.include? c }
# .reject{ |c| ret.include? c }
# break if todo.size == 0
# end
[ret, secs]
end
p get_time g
# XMIN, XMAX = DATA.map(&:first).minmax
# YMIN, YMAX = DATA.map(&:last).minmax
# OBJS = DATA.each_with_index.to_a
# P1 = (XMIN..XMAX).map do |x|
# (YMIN..YMAX).map do |y|
# s = OBJS
# .map{ |a, idx| [(x-a[0]).abs + (y-a[1]).abs, idx] }
# .sort_by{ |a| a[0] }
# s[0][0] == s[1][0] ? nil : s[0][1]
# end
# end
# PART1 = P1
# .flatten
# .group_by(&:itself)
# .map{ |k, v| [k, v.size] }
# .sort_by{ |k, v| v }
# .last.last
# P2 = (XMIN..XMAX).map do |x|
# (YMIN..YMAX).map do |y|
# OBJS
# .map{ |a, idx| (x-a[0]).abs + (y-a[1]).abs }
# .sum
# .yield_self{ |n| n < 10000 }
# end
# end
# PART2 = P2
# .flatten
# .count(&:itself)
# puts 'Part 1: %s' % PART1
# puts 'Part 2: %s' % PART2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment