File tree Expand file tree Collapse file tree 1 file changed +43
-20
lines changed Expand file tree Collapse file tree 1 file changed +43
-20
lines changed Original file line number Diff line number Diff line change @@ -13,34 +13,57 @@ def initialize(input=nil)
13
13
# @example
14
14
# day.part1 => "CMZ"
15
15
def part1
16
- stacks_9000 = parse_stacks
16
+ crane = CrateMover9000 . new ( parse_stacks , @moves )
17
+ crane . follow_process
18
+ crane . top_crates
19
+ end
17
20
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
24
23
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
29
50
end
30
51
31
52
# @example
32
53
# day.part2 => "MCD"
33
54
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
39
59
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
44
67
end
45
68
46
69
# @example
You can’t perform that action at this time.
0 commit comments