Skip to content

Commit c4e90d0

Browse files
author
Robb Kidd
committed
2022 day 5 part 2
1 parent e72d795 commit c4e90d0

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

2022/ruby/day05.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,40 @@ def self.go
77

88
def initialize(input=nil)
99
@input = input || real_input
10-
@stacks = parse_stacks
1110
@moves = parse_moves
1211
end
1312

1413
# @example
1514
# day.part1 => "CMZ"
1615
def part1
16+
stacks_9000 = parse_stacks
17+
1718
@moves
1819
.each { |step|
1920
step[:quantity].times {
20-
@stacks[step[:to]].push( @stacks[step[:from]].pop)
21+
stacks_9000[step[:to]].push( stacks_9000[step[:from]].pop)
2122
}
2223
}
2324

24-
@stacks
25+
stacks_9000
2526
.values
2627
.map(&:last)
2728
.join("")
2829
end
2930

31+
# @example
32+
# day.part2 => "MCD"
3033
def part2
34+
stacks_9001 = parse_stacks
35+
@moves
36+
.each { |step|
37+
stacks_9001[step[:to]] += stacks_9001[step[:from]].pop(step[:quantity])
38+
}
39+
40+
stacks_9001
41+
.values
42+
.map(&:last)
43+
.join("")
3144
end
3245

3346
# @example

0 commit comments

Comments
 (0)