Skip to content

Commit 2d89bd9

Browse files
author
Robb Kidd
committed
embed the only-used-once logic
now the part1 and part2 score_round methods are shaped similarly
1 parent 8c5c428 commit 2d89bd9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

2022/ruby/day02.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,17 @@ def score_round_part1(line)
4141
opp_choice, my_choice = line.split(" ")
4242
opp_shape = OPPONENT_MAP.fetch(opp_choice)
4343
my_shape = PART1_MAP.fetch(my_choice)
44+
45+
outcome = case opp_shape
46+
when my_shape
47+
:draw
48+
when WHAT_LOSES_TO.fetch(my_shape)
49+
:win
50+
else
51+
:lose
52+
end
4453

45-
SHAPE_SCORES.fetch(my_shape) + OUTCOME_SCORES.fetch(against(opp_shape, my_shape))
54+
SHAPE_SCORES.fetch(my_shape) + OUTCOME_SCORES.fetch(outcome)
4655
end
4756

4857
# @example
@@ -68,12 +77,6 @@ def score_round_part2(line)
6877
SHAPE_SCORES.fetch(my_shape) + OUTCOME_SCORES.fetch(target_outcome)
6978
end
7079

71-
def against(opp_shape, my_shape)
72-
return :draw if opp_shape == my_shape
73-
return :win if opp_shape == WHAT_LOSES_TO.fetch(my_shape)
74-
return :lose
75-
end
76-
7780
# the name seems backwards here, but consider:
7881
#
7982
# WHAT_LOSES_TO.fetch(:rock) => :scissors

0 commit comments

Comments
 (0)