Skip to content

Commit e374a61

Browse files
author
Robb Kidd
committed
MOAR OO
1 parent c4e90d0 commit e374a61

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

2022/ruby/day05.rb

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,57 @@ def initialize(input=nil)
1313
# @example
1414
# day.part1 => "CMZ"
1515
def part1
16-
stacks_9000 = parse_stacks
16+
crane = CrateMover9000.new(parse_stacks, @moves)
17+
crane.follow_process
18+
crane.top_crates
19+
end
1720

18-
@moves
19-
.each { |step|
20-
step[:quantity].times {
21-
stacks_9000[step[:to]].push( stacks_9000[step[:from]].pop)
22-
}
23-
}
21+
class CrateMover
22+
attr_reader :stacks, :steps
2423

25-
stacks_9000
26-
.values
27-
.map(&:last)
28-
.join("")
24+
def initialize(stacks, steps)
25+
@stacks = stacks
26+
@steps = steps
27+
end
28+
29+
def follow_process
30+
raise NotImplementedError, "You must have your model of CrateMover available to operate the crane."
31+
end
32+
33+
def top_crates
34+
stacks
35+
.values
36+
.map(&:last)
37+
.join("")
38+
end
39+
end
40+
41+
class CrateMover9000 < CrateMover
42+
def follow_process
43+
steps
44+
.each { |step|
45+
step[:quantity].times {
46+
stacks[step[:to]].push( stacks[step[:from]].pop)
47+
}
48+
}
49+
end
2950
end
3051

3152
# @example
3253
# day.part2 => "MCD"
3354
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-
}
55+
crane = CrateMover9001.new(parse_stacks, @moves)
56+
crane.follow_process
57+
crane.top_crates
58+
end
3959

40-
stacks_9001
41-
.values
42-
.map(&:last)
43-
.join("")
60+
class CrateMover9001 < CrateMover
61+
def follow_process
62+
steps
63+
.each { |step|
64+
stacks[step[:to]] += stacks[step[:from]].pop(step[:quantity])
65+
}
66+
end
4467
end
4568

4669
# @example

0 commit comments

Comments
 (0)