-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2_part1.py
87 lines (73 loc) · 2.33 KB
/
day2_part1.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
instr = {}
rock = 1
paper = 2
scissors = 3
win = 6
draw = 3
lose = 0
totalScore = 0
# list of lists to hold the rules - their move, my move, my outcome (added later), my score (added later)
ruleBook = []
ruleBook.append(['A', 'X'])
ruleBook.append(['A', 'Y'])
ruleBook.append(['A', 'Z'])
ruleBook.append(['B', 'X'])
ruleBook.append(['B', 'Y'])
ruleBook.append(['B', 'Z'])
ruleBook.append(['C', 'X'])
ruleBook.append(['C', 'Y'])
ruleBook.append(['C', 'Z'])
def getOutcome(theirMove, myMove):
outcome = lose
#their rock loses to my paper
if theirMove == 'A' and myMove == 'Y':
outcome = win
#their paper loses to my scissors
if theirMove == 'B' and myMove == 'Z':
outcome = win
#their scissors loses to my rock
if theirMove == 'C' and myMove == 'X':
outcome = win
#rock and rock draws
if theirMove == 'A' and myMove == 'X':
outcome = draw
#paper and paper draws
if theirMove == 'B' and myMove == 'Y':
outcome = draw
#scissors and scissors draws
if theirMove == 'C' and myMove == 'Z':
outcome = draw
return outcome
def getScore(outcome, myMove):
if myMove == 'X':
myMoveNum = rock
if myMove == 'Y':
myMoveNum = paper
if myMove == 'Z':
myMoveNum = scissors
return outcome + myMoveNum
#Calculate the outcomes and scores and populate the rulebook
for move in ruleBook:
outcome = getOutcome(move[0], move[1])
move.append(outcome)
move.append(getScore(outcome, move[1]))
#read in the file values and determine the result
with open('day2_input.txt', 'r') as file:
play = 1
score = 0
for line in file:
key = 'play' + str(play)
cleanLine = line.replace('\n', '')
theirMove, myMove = cleanLine.split(' ')
instr[key] = []
instr[key].append(theirMove)
instr[key].append(myMove)
#iterate through rule book to find the matching outcome and score, populate the record
for rule in ruleBook:
if rule[0] == theirMove and rule[1] == myMove:
instr[key].append(rule[2]) #append the matching outcome
instr[key].append(rule[3]) #append the matching score
play += 1
for game in instr.keys():
totalScore += instr[game][3]
print('Total is: ' + str(totalScore))