Advent of Code 2022 - 2

Day 2: Rock Paper Scissors

https://adventofcode.com/2022/day/2

Part One

原本寫了一堆 if-else,簡化了一下變這樣,可讀性有點低就是了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
shape = {
'A': 1, 'X': 1, # rock
'B': 2, 'Y': 2, # paper
'C': 3, 'Z': 3, # scissors
}
score = 0
with open('02.txt', 'r') as fp:
for line in fp:
line = line.strip()
if len(line):
oppo, mine = line.split(' ')
score += shape[mine]
if shape[oppo] == shape[mine]:
score += 3
elif shape[mine]-shape[oppo] in [1, -2]:
score += 6
print(score)

Answer: 9177

Part Two

可讀性超低呵呵。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
shape = {
'A': 1, # rock
'B': 2, # paper
'C': 3, # scissors
}
guide = {
'X': 0, # lose
'Y': 3, # draw
'Z': 6, # win
}
score = 0
with open('02.txt', 'r') as fp:
for line in fp:
line = line.strip()
if len(line):
oppo, stat = line.split(' ')
score += guide[stat]
if stat == 'Y':
score += shape[oppo]
elif stat == 'X':
score += 3 - (4-shape[oppo])%3
elif stat == 'Z':
score += shape[oppo]%3 + 1
print(score)

Answer: 12111