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

35 lines
823 B
Python
Raw Permalink Normal View History

2022-12-09 16:43:00 +00:00
import shared
import string
def part1(rucksacks):
total = 0
2022-12-12 07:41:14 +00:00
for comp1, comp2 in [(r[: len(r) // 2], r[len(r) // 2 :]) for r in rucksacks]:
2022-12-09 16:43:00 +00:00
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)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
def part2(rucksacks):
total = 0
for idx in range(0, len(rucksacks), 3):
2022-12-12 07:41:14 +00:00
r1, r2, r3 = rucksacks[idx : idx + 3]
2022-12-09 16:43:00 +00:00
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)
2022-12-12 07:41:14 +00:00
2022-12-09 16:43:00 +00:00
if __name__ == "__main__":
main()