[py] Solve 2022_02

This commit is contained in:
Joscha 2022-12-04 14:06:34 +01:00
parent 476d3bcccc
commit f44036b5de
2 changed files with 25 additions and 1 deletions

View file

@ -2,10 +2,11 @@ import sys
import argparse import argparse
from pathlib import Path from pathlib import Path
from .y2022 import d01, d04 from .y2022 import d01, d02, d04
DAYS = { DAYS = {
"2022_01": y2022.d01.solve, "2022_01": y2022.d01.solve,
"2022_02": y2022.d02.solve,
"2022_04": y2022.d04.solve, "2022_04": y2022.d04.solve,
} }

23
py/aoc/y2022/d02.py Normal file
View file

@ -0,0 +1,23 @@
def score(own, elf):
return own + 1 + elf * 3
def find_outcome(elf, own):
return (own - elf + 1) % 3
def find_own(elf, outcome):
return (elf + outcome - 1) % 3
def solve(inputstr):
games = []
for line in inputstr.splitlines():
elf, own = line.split()
elf = ord(elf) - ord("A")
own = ord(own) - ord("X")
games.append((elf, own))
part1 = sum(score(own, find_outcome(elf, own)) for elf, own in games)
part2 = sum(score(find_own(elf, outcome), outcome) for elf, outcome in games)
print(f"Part 1: {part1}")
print(f"Part 2: {part2}")