advent-of-code/2022/python/day03.py

33 lines
808 B
Python
Raw Normal View History

2022-12-09 16:43:00 +00:00
import shared
import string
def part1(rucksacks):
total = 0
for comp1,comp2 in [(r[:len(r)//2], r[len(r)//2:]) for r in rucksacks]:
c1 = set(x for x in comp1)
c2 = set(x for x in comp2)
match = list(c1.intersection(c2))[0]
total += string.ascii_letters.index(match) + 1
print(total)
def part2(rucksacks):
total = 0
for idx in range(0, len(rucksacks), 3):
r1,r2,r3 = rucksacks[idx:idx+3]
c1 = set(x for x in r1)
c2 = set(x for x in r2)
c3 = set(x for x in r3)
match = list(c1.intersection(c2).intersection(c3))[0]
total += string.ascii_letters.index(match) + 1
print(total)
def main():
rows = [row for row in shared.load(3)]
part1(rows)
part2(rows)
if __name__ == "__main__":
main()