-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-1.rb
executable file
·69 lines (56 loc) · 1.35 KB
/
5-1.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
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env ruby
class Ship
def initialize(stacks, last_stack)
@num_stacks = last_stack + 1
@ship = []
@num_stacks.times { |_| @ship.append [] }
stacks.each do |level|
stack = 0
level.chars.each_slice(4) do |slot|
stack += 1
crate = slot[1]
next if crate == ' '
@ship[stack].push crate
end
end
@ship.map(&:reverse!)
end
def move(num, from, to)
num.times do |_|
@ship[to].push @ship[from].pop
end
end
def result
@ship.map { |stack| stack.last or '' }.reduce(:+)
end
def to_s
s = "<#{self.class}:\n"
ship.each_with_index do |stack, i|
next if i.zero?
s += i.to_s + ": " + stack.to_s + "\n"
end
s += ">"
end
def inspect
to_s
end
end
stack_format = /\[/
number_format = /^(( [[:digit:]]+ ) )+( [[:digit:]]+ )$/
move_format = /^move (?<num>[[:digit:]]+) from (?<from>[[:digit:]]+) to (?<to>[[:digit:]]+)$/
input = File.read('5.input').lines
ship = nil
stacks = []
last_stack = 0
input.each do |line|
if stack_format.match line
stacks.append line
elsif number_format.match line
last_stack = line.split[-1].to_i
ship = Ship.new(stacks, last_stack)
elsif (move = move_format.match line.strip)
ship.move(move['num'].to_i, move['from'].to_i, move['to'].to_i)
elsif line == "\n"
end
end
print ship.result, "\n"