-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
162 lines (137 loc) · 4.37 KB
/
day02.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
--- Day 2: Rock Paper Scissors ---
Basically two players are playing rock sizors game
in input column on the left represents the oponent
coulumn on the right represents you. Your task is
to follow rules for specific combinatios and find
out the answer or the final score.
Part one and part two differes on the basis of the
rules that are for calculating the score.
You calculate the score based on you win or loose or what you pick
the scores for win loose or draw are given below:
Loose = 0
Draw = 3
Win = 6
Sample Input Explanation:
In the input below. Left side represent your oponent and the right side
represents you.
A Y
B X
C Z
The ABC and XYZ are associated with rock papers and
sissors like below:
AX = ROCK = 1
BY = PAPER = 2
CZ = SICCSORS = 3
"""
filename = '/Users/ali/Documents/programming-with-python-fork/aoc/day02.txt'
def getdata(filename):
'''
Input: Path of file
Output: List of lists
'''
listOfLists = []
with open(filename) as file:
lines = file.readlines()
for line in lines:
listOfLists.append(line.strip().split(" "))
return listOfLists
def getScore(first, second):
'''
Input: Player one and player two current selection.
Where player first is oponent and
second is you. Also remember the left and
right conditions.
Output: Score for that given condition.
'''
# X scores 1 and Draw Scores 3
if first == 'A' and second == 'X':
return 1 + 3
# Y scores 2 and Win Scores 6
elif first == 'A' and second == 'Y':
return 2 + 6
# Z scores 3 and Loose scores 0
elif first == 'A' and second == 'Z':
return 3 + 0
# X scores 1 and Loose scores 0
elif first == 'B' and second == 'X':
return 1 + 0
# Y scores 2 and Draw scores 3
elif first == 'B' and second == 'Y':
return 2 + 3
# Z scores 3 and Win scores 6
elif first == 'B' and second == 'Z':
return 3 + 6
# X scores 1 and Win scores 6
elif first == 'C' and second == 'X':
return 1 + 6
# Y scores 2 and Loose scores 0
elif first == 'C' and second == 'Y':
return 2 + 0
# Z scores 3 and Draw scores 3
elif first == 'C' and second == 'Z':
return 3 + 3
def getFinalSum(listOfTurns, func):
"""
Input: listOfTurns: Just a simple list
with instructions.
func: name of the function i-e
there are different functions
for part one and for part 2.
Output: Returns the sum of all the scores i-e
the final score.
"""
score = 0
for turn in listOfTurns:
tempScpre = func(turn[0], turn[1])
score += tempScpre
return score
# print(getScore('C', 'Z'))
def getScoreUpdated(first, second):
'''
Input: First is the oponents Pick
Output: loose, draw or win.
It is a little bit twisted this time.
Now, X, Y, Z represent loose, draw and
win respectively. See as follow:
X -> Loose
Y -> Draw
Z -> Win
So the first number in
the return statement is the score that
you get as a result of picking rock,
papers and sizors (i-e, A, B and C).
Second number is the number from the
result of the specific round. Which could be
either X, Y or Z.
'''
# You pick C=3 in order to loose=0
if first == 'A' and second == 'X':
return 3 + 0
# You pick A=1 in order to draw=3
elif first == 'A' and second == 'Y':
return 1 + 3
# You pick B=2 in order to win=6
elif first == 'A' and second == 'Z':
return 2 + 6
# You pick A=1 in order to loose=0
elif first == 'B' and second == 'X':
return 1 + 0
# You pick B=2 in order to draw=3
elif first == 'B' and second == 'Y':
return 2 + 3
# You pick C=3 in order to win=6
elif first == 'B' and second == 'Z':
return 3 + 6
# You pick B=2 in order to loose=0
elif first == 'C' and second == 'X':
return 2 + 0
# You pick C=3 in order to draw=3
elif first == 'C' and second == 'Y':
return 3 + 3
# You pick X=1 in order to win=6
elif first == 'C' and second == 'Z':
return 1 + 6
listOfTurns = getdata(filename)
print('Part One: ', getFinalSum(listOfTurns, getScore))
print('Part Two: ', getFinalSum(listOfTurns, getScoreUpdated))