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-1.py 684B

123456789101112131415161718192021222324252627
  1. from string import ascii_letters
  2. def split_line(line):
  3. line_length = len(line)
  4. section_length = line_length//2
  5. first, second = line[:section_length], line[section_length:]
  6. return first, second
  7. def main():
  8. with open("input3.txt", "r") as file:
  9. inlines = file.readlines()
  10. lines = [(split_line(line)) for line in inlines]
  11. rucksacks = [(set(first), set(second)) for (first, second) in lines]
  12. values = []
  13. for sack in rucksacks:
  14. first, second = sack
  15. common_item = list(first.intersection(second))[0]
  16. values.append(ascii_letters.index(common_item)+1)
  17. print(sum(values))
  18. if __name__ == "__main__":
  19. main()