mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 00:11:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
40
adventofcode/2022/day2/day2.py
Normal file
40
adventofcode/2022/day2/day2.py
Normal file
@@ -0,0 +1,40 @@
|
||||
def main(content):
|
||||
table = str.maketrans("XYZ", "ABC")
|
||||
score = 0
|
||||
for c in content:
|
||||
c = c.translate(table)
|
||||
w = ord(c[-1]) - ord("A") + 1
|
||||
match c:
|
||||
case "A A" | "B B" | "C C": score += 3 + w
|
||||
case "A B" | "B C" | "C A": score += 6 + w
|
||||
case "A C" | "B A" | "C B": score += w
|
||||
case _: assert False, c
|
||||
print("Part 1: ", score)
|
||||
|
||||
# x = lose, y = draw, z = win
|
||||
score = 0
|
||||
for c in content:
|
||||
outcome = c[-1]
|
||||
if outcome == "Y":
|
||||
w = ord(c[0]) - ord("A") + 1
|
||||
score += 3 + w
|
||||
elif outcome == "Z":
|
||||
index = ord(c[0]) - ord("A")
|
||||
play = "ABC"[(index + 1) % 3]
|
||||
w = ord(play) - ord("A") + 1
|
||||
score += 6 + w
|
||||
elif outcome == "X":
|
||||
index = ord(c[0]) - ord("A")
|
||||
w = ord("ABC"[index - 1]) - ord("A") + 1
|
||||
score += w
|
||||
else:
|
||||
assert False, outcome
|
||||
print(score)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import fileinput
|
||||
#main(['A Y', 'B X', 'C Z'])
|
||||
main(list(l.rstrip() for l in fileinput.input()))
|
||||
|
Reference in New Issue
Block a user