-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
executable file
·63 lines (49 loc) · 1.46 KB
/
day2.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
#!/usr/bin/env python3
import sys
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = open(filename).read().strip()
SCORE_WIN = 6
SCORE_DRAW = 3
SCORE_LOOSE = 0
ROCK = 1
PAPER = 2
SCISSORS = 3
NEED_TO_LOOSE = 1
NEED_TO_DRAW = 2
NEED_TO_WIN = 3
part1 = 0
part2 = 0
for i in data.splitlines():
opponent, me = i.split()
opponent = ord(opponent) - ord("A") + 1
me = ord(me) - ord("X") + 1
# part 1
if opponent == me:
part1 += me + SCORE_DRAW
elif opponent == ROCK and me == SCISSORS: # the rock breaks the scissors
part1 += me + SCORE_LOOSE
elif opponent == SCISSORS and me == PAPER: # the scissors cut the paper
part1 += me + SCORE_LOOSE
elif opponent == PAPER and me == ROCK: # the paper covers the scissors
part1 += me + SCORE_LOOSE
else:
part1 += me + SCORE_WIN
# part 2
if me == NEED_TO_DRAW:
part2 += opponent + SCORE_DRAW
elif me == NEED_TO_LOOSE:
if opponent == ROCK:
part2 += SCISSORS + SCORE_LOOSE
elif opponent == PAPER:
part2 += ROCK + SCORE_LOOSE
elif opponent == SCISSORS:
part2 += PAPER + SCORE_LOOSE
else:
if opponent == ROCK:
part2 += PAPER + SCORE_WIN
elif opponent == PAPER:
part2 += SCISSORS + SCORE_WIN
elif opponent == SCISSORS:
part2 += ROCK + SCORE_WIN
print(part1)
print(part2)