Advertisement
GalinaKG

Advent of code - Day 3 - First task

Dec 3rd, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2. duplicate_chars = []
  3. total_sum = 0
  4.  
  5. with open("text", "r") as input_data:
  6.     for line in input_data:
  7.         half = len(line) // 2
  8.         first_half = line[:half]
  9.         second_half = line[half:]
  10.  
  11.         temporary_chars = []
  12.         for ch in first_half:
  13.             if ch in second_half and ch not in temporary_chars:
  14.                 temporary_chars.append(ch)
  15.  
  16.         duplicate_chars.extend(temporary_chars)
  17.  
  18. for ch in duplicate_chars:
  19.     total_sum += alphabet.index(ch) + 1
  20.  
  21. print(total_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement