-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-2.rb
executable file
·59 lines (52 loc) · 1.06 KB
/
13-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
58
59
#!/usr/bin/env ruby
def compare(p1, p2)
left = p1[0]
right = p2[0]
return 0 if left.nil? and right.nil?
return -1 if left.nil?
return 1 if right.nil?
if left.is_a? Integer and right.is_a? Integer
if left == right
compare(p1[1..], p2[1..])
elsif left < right
-1
else
1
end
elsif left.is_a? Array and right.is_a? Array
case compare(left, right)
when 0
compare(p1[1..], p2[1..])
when -1
-1
when 1
1
end
elsif left.is_a? Integer
case compare([left], right)
when 0
compare(p1[1..], p2[1..])
when -1
-1
when 1
1
end
elsif right.is_a? Integer
case compare(left, [right])
when 0
compare(p1[1..], p2[1..])
when -1
-1
when 1
1
end
else
raise "Type error: #{left}, #{right}"
end
end
dividers = [[[2]], [[6]]]
input = File.read('13.input').lines.map(&:strip).map { |l| eval(l) }
input.reject! &:nil?
input += dividers
input.sort! { |a, b| compare(a, b) }
print dividers.map { |d| input.index(d) + 1 }.reduce(:*), "\n"