Skip to content

Commit 8c5c428

Browse files
author
Robb Kidd
committed
names for things
1 parent 4e48e96 commit 8c5c428

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

2022/ruby/day02.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,39 @@ def score_round_part2(line)
5555
opp_choice, round_end = line.split(" ")
5656
opp_shape = OPPONENT_MAP.fetch(opp_choice)
5757
target_outcome = PART2_MAP.fetch(round_end)
58+
5859
my_shape = case target_outcome
5960
when :draw
6061
opp_shape
6162
when :win
62-
DEFEATED_BY.fetch(opp_shape)
63+
WHAT_DEFEATS.fetch(opp_shape)
6364
when :lose
64-
DEFEATS.fetch(opp_shape)
65+
WHAT_LOSES_TO.fetch(opp_shape)
6566
end
6667

6768
SHAPE_SCORES.fetch(my_shape) + OUTCOME_SCORES.fetch(target_outcome)
6869
end
6970

7071
def against(opp_shape, my_shape)
7172
return :draw if opp_shape == my_shape
72-
return :win if opp_shape == DEFEATS.fetch(my_shape)
73+
return :win if opp_shape == WHAT_LOSES_TO.fetch(my_shape)
7374
return :lose
7475
end
7576

76-
DEFEATS = {
77+
# the name seems backwards here, but consider:
78+
#
79+
# WHAT_LOSES_TO.fetch(:rock) => :scissors
80+
#
81+
# or "What is defeated by rock?" -> "scissors"
82+
WHAT_LOSES_TO = {
7783
rock: :scissors,
7884
scissors: :paper,
7985
paper: :rock,
8086
}
8187

82-
DEFEATED_BY = DEFEATS.invert
88+
# ... and then we invert it so we can look up the
89+
# other direction
90+
WHAT_DEFEATS = WHAT_LOSES_TO.invert
8391

8492
OPPONENT_MAP = {
8593
'A' => :rock,

0 commit comments

Comments
 (0)