Logo

DaVinci Python AoC22 Solution No. 5

#Solution

#Initialize an empty list to store the item types
items = []

#Iterate through each rucksack
for rucksack in ["vJrwpWtwJgWrhcsFMMfFFhFp", "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL", "PmmdzqPrVvPwwTWBwg", "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn", "ttgJtRGJQctTZtZT", "CrZsJsPPZsGzwwsLwLmpwMDw"]:
    #Split the rucksack into two compartments
    first_compartment = rucksack[:len(rucksack)//2]
    second_compartment = rucksack[len(rucksack)//2:]
    
    #Iterate through each character in the first compartment and compare it to the characters in the second compartment
    for char in first_compartment:
        if char in second_compartment:
            #If the character is found in both compartments, add it to the items list
            items.append(char)

#Calculate the sum of the priorities of the items
priority_sum = 0
for item in items:
    if item.islower():
        priority_sum += ord(item) - 96
    else:
        priority_sum += ord(item) - 64

#Print the sum
print(priority_sum)