@@ -15,24 +15,58 @@ def part1
15
15
@input
16
16
. split ( "\n " )
17
17
. map { |line |
18
- score_round ( line )
18
+ score_round_part1 ( line )
19
19
}
20
20
. reduce ( &:+ )
21
21
end
22
22
23
+ # @example
24
+ # day.part2 #=> 12
23
25
def part2
26
+ @input
27
+ . split ( "\n " )
28
+ . map { |line |
29
+ score_round_part2 ( line )
30
+ }
31
+ . reduce ( &:+ )
24
32
end
25
33
26
34
# @example
27
- # day.score_round('A Y') #=> 8
28
- def score_round ( line )
35
+ # day.score_round_part1('A Y') #=> 8
36
+ # @example
37
+ # day.score_round_part1('B X') #=> 1
38
+ # @example
39
+ # day.score_round_part1('C Z') #=> 6
40
+ def score_round_part1 ( line )
29
41
opp_choice , my_choice = line . split ( " " )
30
42
opp_shape = OPPONENT_MAP . fetch ( opp_choice )
31
- my_shape = MY_MAP . fetch ( my_choice )
43
+ my_shape = PART1_MAP . fetch ( my_choice )
32
44
33
45
SHAPE_SCORES . fetch ( my_shape ) + OUTCOME_SCORES . fetch ( against ( opp_shape , my_shape ) )
34
46
end
35
47
48
+ # @example
49
+ # day.score_round_part2('A Y') #=> 4
50
+ # @example
51
+ # day.score_round_part2('B X') #=> 1
52
+ # @example
53
+ # day.score_round_part2('C Z') #=> 7
54
+ def score_round_part2 ( line )
55
+ opp_choice , round_end = line . split ( " " )
56
+ opp_shape = OPPONENT_MAP . fetch ( opp_choice )
57
+ target_outcome = PART2_MAP . fetch ( round_end )
58
+ my_shape = case target_outcome
59
+ when :draw
60
+ opp_shape
61
+ when :win
62
+ DEFEATED_BY . fetch ( opp_shape )
63
+ when :lose
64
+ DEFEATS . fetch ( opp_shape )
65
+ end
66
+
67
+ SHAPE_SCORES . fetch ( my_shape ) + OUTCOME_SCORES . fetch ( target_outcome )
68
+ end
69
+
36
70
def against ( opp_shape , my_shape )
37
71
return :draw if opp_shape == my_shape
38
72
return :win if opp_shape == DEFEATS . fetch ( my_shape )
@@ -53,12 +87,18 @@ def against(opp_shape, my_shape)
53
87
'C' => :scissors ,
54
88
}
55
89
56
- MY_MAP = {
90
+ PART1_MAP = {
57
91
'X' => :rock ,
58
92
'Y' => :paper ,
59
93
'Z' => :scissors ,
60
94
}
61
95
96
+ PART2_MAP = {
97
+ 'X' => :lose ,
98
+ 'Y' => :draw ,
99
+ 'Z' => :win ,
100
+ }
101
+
62
102
SHAPE_SCORES = {
63
103
rock : 1 ,
64
104
paper : 2 ,
0 commit comments