Advent of Code 2022 https://adventofcode.com/2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

day3-2.py 779B

123456789101112131415161718192021222324252627282930313233343536
  1. from string import ascii_letters
  2. DEBUG=False
  3. def debug(*args):
  4. if DEBUG:
  5. print(args)
  6. def main():
  7. with open("input3.txt", "r") as file:
  8. inlines = [line.strip() for line in file.readlines()]
  9. groups = []
  10. total_elves = len(inlines)
  11. i,j,k = 0,1,2
  12. while i < total_elves:
  13. groups.append([inlines[i], inlines[j], inlines[k]])
  14. i, j, k = i+3, j+3, k+3
  15. values = []
  16. for group in groups:
  17. debug(group)
  18. first, second, third = [set(elf) for elf in group]
  19. debug(first, second, third)
  20. debug(first & second & third)
  21. common_item = list(first & second & third)[0]
  22. values.append(ascii_letters.index(common_item)+1)
  23. print(sum(values))
  24. if __name__ == "__main__":
  25. main()