Add 2022 day 2

This commit is contained in:
Søren Rasmussen 2022-12-02 09:40:51 +01:00
parent c08fea2ade
commit 477c54e1a8
4 changed files with 2574 additions and 0 deletions

2500
2022/day_2/data.txt Normal file

File diff suppressed because it is too large Load diff

3
2022/day_2/demo.txt Normal file
View file

@ -0,0 +1,3 @@
A Y
B X
C Z

33
2022/day_2/part1.py Normal file
View file

@ -0,0 +1,33 @@
ahand = ("A", "B", "C")
bhand = ("X", "Y", "Z")
win_tbl = (
("A", "Y"),
("B", "Z"),
("C", "X"),
)
def load(filename):
with open(filename) as f:
t = 0
for l in f:
a, b = l.rstrip().split(" ")
h = bhand.index(b) + 1
ai = ahand.index(a)
bi = bhand.index(b)
if ai == bi:
t += h + 3
elif (a, b) in win_tbl:
t += h + 6
else:
t += h
return t
assert load("demo.txt") == 15
t = load("data.txt")
print(f"You have {t} points.")

38
2022/day_2/part2.py Normal file
View file

@ -0,0 +1,38 @@
ahand = ("A", "B", "C")
bhand = ("X", "Y", "Z")
win_tbl = {
"A": "B",
"B": "C",
"C": "A",
}
defeats_tbl = {
"A": "C",
"B": "A",
"C": "B",
}
def load(filename):
with open(filename) as f:
t = 0
for l in f:
a, b = l.rstrip().split(" ")
if b == "X":
h = ahand.index(defeats_tbl[a]) + 1
t += h
elif b == "Y":
h = ahand.index(a) + 1
t += h + 3
elif b == "Z":
h = ahand.index(win_tbl[a]) + 1
t += h + 6
return t
assert load("demo.txt") == 12
t = load("data.txt")
print("Total points: ", t)