Logo

DaVinci Python AoC22 Solution No. 2

#Code

# Define the list of rucksack contents
rucksack_contents = ["vJrwpWtwJgWrhcsFMMfFFhFp",
"jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
"PmmdzqPrVvPwwTWBwg",
"wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn",
"ttgJtRGJQctTZtZT",
"CrZsJsPPZsGzwwsLwLmpwMDw"]

# Initialize the sum of priorities
sum_of_priorities = 0

# Iterate through the list of rucksack contents
for rucksack in rucksack_contents:
  # Split the contents into two compartments
  first_compartment = rucksack[:len(rucksack)//2]
  second_compartment = rucksack[len(rucksack)//2:]
  
  # Iterate through the characters of the first compartment
  for char in first_compartment:
    # Check if the character appears in both compartments
    if char in second_compartment:
      # Calculate the priority of the character
      priority = ord(char.lower()) - ord('a') + 1
      
      # Add the priority to the sum of priorities
      sum_of_priorities += priority
      
# Print the sum of priorities
print(sum_of_priorities)