-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.rb
49 lines (35 loc) · 819 Bytes
/
day09.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
def decompress(input)
output = ''
until input.empty?
if input[0] == '('
ending = input.index(')')
a, b = input[1..ending-1].join.split('x').map(&:to_i)
input = input.drop(ending + 1)
output += (input[0...a] * b).join
input = input.drop(a)
else
output += input.shift
end
end
output.size
end
def decompress2(input)
return input.size unless input.index('(')
sum = 0
until input.empty?
if input[0] == '('
ending = input.index(')')
a, b = input[1..ending-1].join.split('x').map(&:to_i)
input = input.drop(ending + 1)
sum += decompress2(input[0...a]) * b
input = input.drop(a)
else
input.shift
sum += 1
end
end
sum
end
s = STDIN.read.strip
puts decompress(s.chars)
puts decompress2(s.chars)