[py] Solve 2022_03
This commit is contained in:
parent
f44036b5de
commit
d6f8ecf700
2 changed files with 25 additions and 1 deletions
|
|
@ -2,11 +2,12 @@ import sys
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .y2022 import d01, d02, d04
|
from .y2022 import d01, d02, d03, d04
|
||||||
|
|
||||||
DAYS = {
|
DAYS = {
|
||||||
"2022_01": y2022.d01.solve,
|
"2022_01": y2022.d01.solve,
|
||||||
"2022_02": y2022.d02.solve,
|
"2022_02": y2022.d02.solve,
|
||||||
|
"2022_03": y2022.d03.solve,
|
||||||
"2022_04": y2022.d04.solve,
|
"2022_04": y2022.d04.solve,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
23
py/aoc/y2022/d03.py
Normal file
23
py/aoc/y2022/d03.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
def priority(item):
|
||||||
|
return ord(item.lower()) - ord("a") + 1 + item.isupper() * 26
|
||||||
|
|
||||||
|
|
||||||
|
def chunks(things, n):
|
||||||
|
return (things[i : i + n] for i in range(0, len(things), n))
|
||||||
|
|
||||||
|
|
||||||
|
def solve(inputstr):
|
||||||
|
rucksacks = inputstr.splitlines()
|
||||||
|
|
||||||
|
part1 = 0
|
||||||
|
for rucksack in rucksacks:
|
||||||
|
half = len(rucksack) // 2
|
||||||
|
common = set(rucksack[:half]) & set(rucksack[half:])
|
||||||
|
part1 += priority(common.pop())
|
||||||
|
print(f"Part 1: {part1}")
|
||||||
|
|
||||||
|
part2 = 0
|
||||||
|
for r1, r2, r3 in chunks(rucksacks, 3):
|
||||||
|
common = set(r1) & set(r2) & set(r3)
|
||||||
|
part2 += priority(common.pop())
|
||||||
|
print(f"Part 2: {part2}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue