Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 4, 2022 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnyman/80505ad67560dbb387dd97f3d7b58100 to your computer and use it in GitHub Desktop.
Save gnyman/80505ad67560dbb387dd97f3d7b58100 to your computer and use it in GitHub Desktop.
import sys
total = 0
# Read the input file from the command line arguments
with open(sys.argv[1], "r") as f:
# Initialize a list to store the lines in groups of three
group = []
# Read each line of the input file
for line in f:
# Add the line to the current group
group.append(line)
# If the group has three lines, process it
if len(group) == 3:
# Initialize a set to keep track of which characters have already been added to the total
added = set()
# Iterate through each character in the first line of the group
for ch in group[0]:
# If the character also appears in the other lines,
# and has not already been added to the total,
# add its priority to the running total
if all(ch in line for line in group[1:]) and ch not in added:
# 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
# Mark the character as added
added.add(ch)
# Clear the group
group = []
# Print the sum of the priorities of the item types that correspond to the badges
# of each group
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment