Created
December 4, 2022 14:02
-
-
Save gnyman/1e2bf752e09e341f29e42330fe5e491b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
total = 0 | |
# Read the input file from the command line arguments | |
with open(sys.argv[1], "r") as f: | |
# Read each line of the input file | |
for line in f: | |
# Split the line into two compartments | |
comp1, comp2 = line[:len(line)//2], line[len(line)//2:] | |
# Iterate through each character in the first compartment | |
for ch in comp1: | |
# If the character also appears in the second compartment, | |
# add its priority to the running total | |
if ch in comp2: | |
# Lowercase item types have priorities 1 through 26 | |
if ch.islower(): | |
total += ord(ch) - ord("a") + 1 | |
# Uppercase item types have priorities 27 through 52 | |
elif ch.isupper(): | |
total += ord(ch) - ord("A") + 27 | |
# Print the sum of the priorities of the item types that appear in both | |
# compartments of each rucksack | |
print(total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment