Add comments to solutions so far
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
# Import lists of the alphabet so we can find indxes later.
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
|
||||
commonItems = []
|
||||
|
||||
# For each backpack, split the contents into two lists and find the common item.
|
||||
with open('input.txt', 'r') as inputFile:
|
||||
for backpack in inputFile:
|
||||
items = [*backpack.strip()]
|
||||
@@ -19,12 +21,13 @@ with open('input.txt', 'r') as inputFile:
|
||||
commonItems.append(item)
|
||||
break
|
||||
|
||||
# For all of the common items we found, total up the priorities.
|
||||
totalPriority = 0
|
||||
|
||||
for item in commonItems:
|
||||
if(item in ascii_lowercase):
|
||||
totalPriority += (ascii_lowercase.index(item) + 1)
|
||||
elif(item in ascii_uppercase):
|
||||
totalPriority += (ascii_uppercase.index(item) + 27)
|
||||
|
||||
# Print the answer.
|
||||
print(totalPriority)
|
@@ -1,15 +1,17 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
# Import lists of the alphabet so we can find indxes later.
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
|
||||
commonItems = []
|
||||
backpacks = []
|
||||
|
||||
# Read the whole input file to a list.
|
||||
with open('input.txt', 'r') as inputFile:
|
||||
backpacks = inputFile.readlines()
|
||||
|
||||
# Group the backpacks up into threes and find the common item in each three.
|
||||
currentBackpack = 0
|
||||
|
||||
while(currentBackpack < len(backpacks)):
|
||||
currentGroup = []
|
||||
while(len(currentGroup) < 3):
|
||||
@@ -20,12 +22,13 @@ while(currentBackpack < len(backpacks)):
|
||||
commonItems.append(item)
|
||||
break
|
||||
|
||||
# For all of the common items we found, total up the priorities.
|
||||
totalPriority = 0
|
||||
|
||||
for item in commonItems:
|
||||
if(item in ascii_lowercase):
|
||||
totalPriority += (ascii_lowercase.index(item) + 1)
|
||||
elif(item in ascii_uppercase):
|
||||
totalPriority += (ascii_uppercase.index(item) + 27)
|
||||
|
||||
# Print the answer.
|
||||
print(totalPriority)
|
||||
|
Reference in New Issue
Block a user