97 lines
2.2 KiB
Python
97 lines
2.2 KiB
Python
|
import re
|
||
|
|
||
|
EXAMPLE = """
|
||
|
A Y
|
||
|
B X
|
||
|
C Z
|
||
|
"""
|
||
|
|
||
|
def clean(text: str) -> list[str]:
|
||
|
return list(filter(lambda l: l.strip() != "", text.splitlines()))
|
||
|
# return list(text.splitlines()))
|
||
|
|
||
|
def solve(lines: list[str]):
|
||
|
s = 0
|
||
|
for (i, line) in enumerate(lines):
|
||
|
a, b = line.split(" ")
|
||
|
if b == "X":
|
||
|
s += 1
|
||
|
if a == "A":
|
||
|
s += 3
|
||
|
elif a == "B":
|
||
|
s += 0
|
||
|
elif a == "C":
|
||
|
s += 6
|
||
|
else:
|
||
|
print("invalid")
|
||
|
elif b == "Y":
|
||
|
s += 2
|
||
|
if a == "A":
|
||
|
s += 6
|
||
|
elif a == "B":
|
||
|
s += 3
|
||
|
elif a == "C":
|
||
|
s += 0
|
||
|
else:
|
||
|
print("invalid")
|
||
|
elif b == "Z":
|
||
|
s += 3
|
||
|
if a == "A":
|
||
|
s += 0
|
||
|
elif a == "B":
|
||
|
s += 6
|
||
|
elif a == "C":
|
||
|
s += 3
|
||
|
else:
|
||
|
print("invalid")
|
||
|
return s
|
||
|
|
||
|
def solve2(lines: list[str]):
|
||
|
s = 0
|
||
|
for (i, line) in enumerate(lines):
|
||
|
a, b = line.split(" ")
|
||
|
if a == "A":
|
||
|
if b == "X": # lose
|
||
|
s += 3
|
||
|
elif b == "Y": # draw
|
||
|
s += 4
|
||
|
elif b == "Z": # win
|
||
|
s += 8
|
||
|
else:
|
||
|
print("invalid")
|
||
|
elif a == "B":
|
||
|
if b == "X": # lose
|
||
|
s += 1
|
||
|
elif b == "Y": # draw
|
||
|
s += 5
|
||
|
elif b == "Z": # win
|
||
|
s += 9
|
||
|
else:
|
||
|
print("invalid")
|
||
|
elif a == "C":
|
||
|
if b == "X": # lose
|
||
|
s += 2
|
||
|
elif b == "Y": # draw
|
||
|
s += 6
|
||
|
elif b == "Z": # win
|
||
|
s += 7
|
||
|
else:
|
||
|
print("invalid")
|
||
|
return s
|
||
|
|
||
|
def main():
|
||
|
example = clean(EXAMPLE)
|
||
|
print("Example 1:", solve(example))
|
||
|
|
||
|
data = clean(open("i2.txt").read())
|
||
|
print("Solution 1:", solve(data))
|
||
|
|
||
|
example = clean(EXAMPLE)
|
||
|
print("Example 2:", solve2(example))
|
||
|
|
||
|
data = clean(open("i2.txt").read())
|
||
|
print("Solution 2:", solve2(data))
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|