aboutsummaryrefslogtreecommitdiff
path: root/day13/part2.rb
blob: 2a3bc0ce7e4599bb33561a6d499614b4da8f9aaa (plain)
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
def compare(left, right)
  if left.is_a?(Numeric) && right.is_a?(Numeric)
    left <=> right
  elsif left.is_a?(Array) && right.is_a?(Array)
    i = 0
    t = nil
    loop do
      if left[i].nil? && right[i].nil?
        t = 0
        break
      elsif left[i].nil?
        t = -1
        break
      elsif right[i].nil?
        t = 1
        break
      else
        t = compare(left[i], right[i])
        if t == 0
          i += 1
        else
          break
        end
      end
    end
    return t
  elsif left.is_a?(Array) && right.is_a?(Numeric)
    return compare(left, [right])
  elsif left.is_a?(Numeric) && right.is_a?(Array)
    return compare([left], right)
  end
end

packets = $stdin.readlines.map(&:strip).reject(&:empty?).map{ |l| eval(l) } + [[[2]], [[6]]]

packets.sort! do |left, right|
  compare(left, right)
end

puts (packets.index([[2]]) + 1) * (packets.index([[6]]) + 1)