Created
December 2, 2022 15:54
-
-
Save joshbduncan/e2b489747c268a253142390f4a2f47d8 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def score_round(elf_idx, me_idx): | |
if elf_idx == me_idx: # draw | |
return me_idx + 1 + 3 | |
elif me_idx % 3 != (elf_idx + 2) % 3: # win | |
return me_idx + 1 + 6 | |
else: | |
return me_idx + 1 # lose | |
data = open("day2.in").read() | |
elf_plays = ["A", "B", "C"] | |
me_plays = ["X", "Y", "Z"] | |
p1_score = 0 | |
p2_score = 0 | |
for row in data.strip().split("\n"): | |
elf, me = row.split() | |
elf_idx, me_idx = elf_plays.index(elf), me_plays.index(me) | |
# part 1 | |
p1_score += score_round(elf_idx, me_idx) | |
# part 2 | |
if me_idx == 0: | |
p2_score += score_round(elf_idx, (elf_idx + 2) % 3) | |
elif me_idx == 1: | |
p2_score += score_round(elf_idx, elf_idx) | |
else: | |
p2_score += score_round(elf_idx, (elf_idx + 1) % 3) | |
print(f"Part 1: {p1_score}") | |
print(f"Part 2: {p2_score}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment