-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.rb
49 lines (37 loc) · 842 Bytes
/
day2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
LOSS = 0
DRAW = 3
WIN = 6
MOVE_POINTS = { rock: 1, paper: 2, scissors: 3 }.freeze
ORDER = %i[rock paper scissors].freeze
def rps_win(move1, move2)
return DRAW if move1 == move2
idx = ORDER.index(move1)
return move2 == ORDER[(idx + 1) % ORDER.length] ? WIN : LOSS
end
def score(move1, move2)
MOVE_POINTS[move2] + rps_win(move1, move2)
end
def winning_move(move1, me)
idx = ORDER.index(move1)
case me
when 'X'
ORDER[idx - 1]
when 'Y'
move1
when 'Z'
ORDER[(idx + 1) % ORDER.length]
end
end
file = File.open('input2.txt')
total1 = 0
total2 = 0
file.readlines.each do |line|
opponent, me = line.split(' ')
move1 = ORDER[opponent.ord - 'A'.ord]
move2 = ORDER[me.ord - 'X'.ord]
total1 += score(move1, move2)
total2 += score(move1, winning_move(move1, me))
end
file.close
puts total1
puts total2