-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 5 - part 2.rb
57 lines (45 loc) · 1.28 KB
/
Day 5 - part 2.rb
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
53
54
55
56
57
# file = File.open(Rails.root.join('tmp','printqueuesmall'))
file = File.open(Rails.root.join('tmp','printqueue'))
raw_queue = []
raw_updated = []
file.each do |line|
raw_queue << line.gsub("\n", '') if line.include? '|'
raw_updated << line.gsub("\n", '') if line.include? ','
end
queue = {}
raw_queue.each do |line|
parts = line.split('|')
queue[parts[0].to_i] ||= []
queue[parts[0].to_i] << parts[1].to_i
end
update_lines = raw_updated.map { |line| line.split(',').map(&:to_i) }
sum = 0
invalid_update_lines = []
update_lines.each do |update_line|
invalid = false
update_line.each_cons(2) do |update|
if update[0].in?(queue[update[1]] || [])
invalid = true
break
end
end
if invalid
invalid_update_lines << update_line
next
end
sum += update_line[update_line.length/2]
end
corrected_update_lines = []
invalid_update_lines.each do |invalid_update_line|
ranks = {}
invalid_update_line.each do |update|
ranks[update] ||= 0
order = queue[update] || []
ranks[update] += (order & (invalid_update_line - [update])).length
end
corrected_update_lines << Hash[ranks.sort_by {|_key, value| value}].keys.reverse
end
corrected_sum = 0
corrected_update_lines.each do |update_line|
corrected_sum += update_line[update_line.length/2]
end