-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.rb
49 lines (41 loc) · 1.07 KB
/
day5.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
input = 'input/day5.txt'
stacks = []
f = open(input, 'r')
f.readlines.each do |line|
line.rstrip!
next if line.empty?
if line.start_with?('move')
count, from, to = line.split.slice((1..5).step(2)).map(&:to_i)
(1..count).each { |_| stacks[to - 1].push(stacks[from - 1].pop) }
else
n = (line.length + 1) / 4
stacks << [] while n > stacks.length
(0..(n - 1)).each do |i|
c = line[4 * i + 1]
next if c == ' '
stacks[i].insert(0, c)
end
end
end
f.close
puts (1..stacks.length).map { |i| stacks[i - 1].pop }.join('')
stacks = []
f = open(input, 'r')
f.readlines.each do |line|
line.rstrip!
next if line.empty?
if line.start_with?('move')
count, from, to = line.split.slice((1..5).step(2)).map(&:to_i)
stacks[to - 1].push(*stacks[from - 1].pop(count))
else
n = (line.length + 1) / 4
stacks << [] while n > stacks.length
(0..(n - 1)).each do |i|
c = line[4 * i + 1]
next if c == ' '
stacks[i].insert(0, c)
end
end
end
f.close
puts (1..stacks.length).map { |i| stacks[i - 1].pop }.join('')