-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlen_out_of_range.rl
More file actions
54 lines (43 loc) · 1.07 KB
/
len_out_of_range.rl
File metadata and controls
54 lines (43 loc) · 1.07 KB
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
instructions = [
"move 1 from 2 to 1",
"move 3 from 1 to 3",
"move 2 from 2 to 1",
"move 1 from 1 to 2"]
stacks = [
["Z", "N"],
["M", "C", "D"],
["P"]
]
stacks2 = stacks
foreach instruction in instructions
amount = instruction.split("from")[0].split("move")[-1].strip().to_i()
from = instruction.split("to")[0].split("from")[-1].strip().to_i()
to = instruction.split("to")[1].strip().to_i()
foreach i in amount
stacks[to - 1].push(stacks[from - 1].pop())
end
end
result = ""
foreach stack in stacks
result = result + stack[-1]
end
puts("Part 1: " + result)
foreach instruction in instructions
amount = instruction.split("from")[0].split("move")[-1].strip().to_i()
from = instruction.split("to")[0].split("from")[-1].strip().to_i()
to = instruction.split("to")[1].strip().to_i()
temp_stack = []
foreach i in amount
temp_stack.push(stacks2[from - 1].pop())
end
temp_stack.reverse()
foreach item in temp_stack
stacks2[to - 1].push(item)
end
end
result = ""
foreach stack in stacks
result = result + stack[-1]
end
puts("Part 2: " + result)
nil