-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13part2.rb
59 lines (55 loc) · 1.18 KB
/
day13part2.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
58
59
file = File.open "day13in.txt"
packets = []
until file.eof?
packets << file.take_while { |l| not l.chomp.empty? }
end
file.close
def compare(a, b)
if a.is_a? Integer and b.is_a? Integer
return 0 if a == b
return 1 if a < b
return -1 if a > b
elsif a.is_a? Array and b.is_a? Array
i = 0
while i < a.length
if i >= b.length
return -1 # b runs out of items
end
res = compare(a[i], b[i])
return res unless res.zero?
i += 1
end
return 1 if i < b.length # b longer means correct order
return 0
elsif a.is_a? Integer
compare([a], b)
else
compare(a, [b])
end
end
class Packet
@arr
attr_reader :arr
def initialize(arr)
@arr = arr
end
def <=>(other)
compare(self.arr, other.arr)
end
end
clean_packets = [Packet.new([[2]]), Packet.new([[6]])]
packets.each_with_index do |packet|
first = Packet.new(eval(packet[0]))
second = Packet.new(eval(packet[1]))
clean_packets << first
clean_packets << second
end
i = 1
clean_packets.sort.reverse.collect { |p| p.arr }.each_with_index do |elem, index|
if elem == [[2]]
i = index + 1
elsif elem == [[6]]
puts i * (index + 1)
break
end
end