Advent of Code 2022 Day 2 Solve

preview_player
Показать описание
Recording of me solving day 2 of Advent of Code 2022 in Python. Finished rank #33 and #38 for each part.

I have somewhat mixed feelings about today: I still did very well (top 40 on both parts!), and it improved my global leaderboard ranking, but as you can probably tell from the video, I wasn't very happy with my code. I feel like doing the integers-mod-3 trick or something similar would've been much cleaner and just as fast if not faster to write, and I think I should've been able to come up with this on the spot.

On another note, starting with today's puzzle I'm going to try to give some explanation and commentary after solving. It probably won't be very much for days like this ("Just implement rock paper scissors"), but on some of the later days I might have something more interesting to say? We'll see!

0:00 Part 1
3:14 Part 2
5:29 Commentary
Рекомендации по теме
Комментарии
Автор

O1 = "ABC"
O2 = "XYZ"

def score(o1, o2):
i1 = O1.index(o1)
i2 = O2.index(o2)
score = i2 + 1
if i1 == i2:
# draw
score += 3
elif (i2 - i1 + 3) % 3 == 1:
# win
score += 6
elif (i2 - i1 + 3) % 3 == 2:
# loose
score += 0
return score

def play(a, instr):
i1 = O1.index(a)
if instr == "Y":
# draw
i2 = i1
elif instr == "X":
# loose
i2 = (i1 + 2) % 3
elif instr == "Z":
# win
i2 = (i1 + 1) % 3
else:
assert False, instr
return O2[i2]

rastislavsvoboda