Add comments to solutions so far

This commit is contained in:
2022-12-03 12:44:18 +00:00
parent fef5053f4d
commit 0a543ee446
6 changed files with 25 additions and 7 deletions

View File

@@ -3,6 +3,7 @@
total = 0 total = 0
elves = [] elves = []
# Add each line of the input, when we hit a blank line put the current total in a list.
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
for line in inputFile: for line in inputFile:
if line == '\n': if line == '\n':
@@ -11,4 +12,5 @@ with open('input.txt', 'r') as inputFile:
else: else:
total += int(line) total += int(line)
# Print the largest number in the list.
print(max(elves)) print(max(elves))

View File

@@ -3,6 +3,7 @@
total = 0 total = 0
elves = [] elves = []
# Add each line of the input, when we hit a blank line put the current total in a list.
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
for line in inputFile: for line in inputFile:
if line == '\n': if line == '\n':
@@ -11,9 +12,11 @@ with open('input.txt', 'r') as inputFile:
else: else:
total += int(line) total += int(line)
# Find the total of the largest three items in the list.
topThree = 0 topThree = 0
for i in range(3): for i in range(3):
topThree += max(elves) topThree += max(elves)
elves.remove(max(elves)) elves.remove(max(elves))
# Print the answer.
print(topThree) print(topThree)

View File

@@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
# Define points values for each move and outcome.
points = { points = {
'rock': 1, 'rock': 1,
'paper': 2, 'paper': 2,
@@ -9,6 +10,7 @@ points = {
'win': 6 'win': 6
} }
# Map letters to moves.
shapes = { shapes = {
'A': 'rock', 'A': 'rock',
'B': 'paper', 'B': 'paper',
@@ -18,6 +20,7 @@ shapes = {
'Z': 'scissors' 'Z': 'scissors'
} }
# Calculate whether we won or lost for a given pair of moves.
def outcome(myMove, yourMove): def outcome(myMove, yourMove):
wins = { wins = {
'rock': 'scissors', 'rock': 'scissors',
@@ -31,8 +34,8 @@ def outcome(myMove, yourMove):
else: else:
return 'lose' return 'lose'
# For each set of moves in the input, work out our total score.
totalScore = 0 totalScore = 0
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
for line in inputFile: for line in inputFile:
moves = line.strip().split(' ') moves = line.strip().split(' ')
@@ -41,4 +44,5 @@ with open('input.txt', 'r') as inputFile:
totalScore += points[ourMove] totalScore += points[ourMove]
totalScore += points[outcome(ourMove, theirMove)] totalScore += points[outcome(ourMove, theirMove)]
# Print the answer.
print(totalScore) print(totalScore)

View File

@@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
# Define points values for each move and outcome.
points = { points = {
'rock': 1, 'rock': 1,
'paper': 2, 'paper': 2,
@@ -9,27 +10,29 @@ points = {
'win': 6 'win': 6
} }
# Map letters to moves.
shapes = { shapes = {
'A': 'rock', 'A': 'rock',
'B': 'paper', 'B': 'paper',
'C': 'scissors', 'C': 'scissors',
} }
# Define what move will win/lose for a given move.
wins = { wins = {
'rock': ['paper', 'scissors'], 'rock': ['paper', 'scissors'],
'paper': ['scissors', 'rock'], 'paper': ['scissors', 'rock'],
'scissors': ['rock', 'paper'] 'scissors': ['rock', 'paper']
} }
# Map letters to the outcome they represent.
outcomes = { outcomes = {
'X': 'lose', 'X': 'lose',
'Y': 'draw', 'Y': 'draw',
'Z': 'win' 'Z': 'win'
} }
# For each line in the input, work out what move we require and what our score would be.
totalScore = 0 totalScore = 0
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
for line in inputFile: for line in inputFile:
moves = line.strip().split(' ') moves = line.strip().split(' ')
@@ -44,5 +47,5 @@ with open('input.txt', 'r') as inputFile:
totalScore += (points[requiredOutcome] + points[requiredMove]) totalScore += (points[requiredOutcome] + points[requiredMove])
# Print the answer.
print(totalScore) print(totalScore)

View File

@@ -1,9 +1,11 @@
#!/usr/bin/python3 #!/usr/bin/python3
# Import lists of the alphabet so we can find indxes later.
from string import ascii_lowercase, ascii_uppercase from string import ascii_lowercase, ascii_uppercase
commonItems = [] commonItems = []
# For each backpack, split the contents into two lists and find the common item.
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
for backpack in inputFile: for backpack in inputFile:
items = [*backpack.strip()] items = [*backpack.strip()]
@@ -19,12 +21,13 @@ with open('input.txt', 'r') as inputFile:
commonItems.append(item) commonItems.append(item)
break break
# For all of the common items we found, total up the priorities.
totalPriority = 0 totalPriority = 0
for item in commonItems: for item in commonItems:
if(item in ascii_lowercase): if(item in ascii_lowercase):
totalPriority += (ascii_lowercase.index(item) + 1) totalPriority += (ascii_lowercase.index(item) + 1)
elif(item in ascii_uppercase): elif(item in ascii_uppercase):
totalPriority += (ascii_uppercase.index(item) + 27) totalPriority += (ascii_uppercase.index(item) + 27)
# Print the answer.
print(totalPriority) print(totalPriority)

View File

@@ -1,15 +1,17 @@
#!/usr/bin/python3 #!/usr/bin/python3
# Import lists of the alphabet so we can find indxes later.
from string import ascii_lowercase, ascii_uppercase from string import ascii_lowercase, ascii_uppercase
commonItems = [] commonItems = []
backpacks = [] backpacks = []
# Read the whole input file to a list.
with open('input.txt', 'r') as inputFile: with open('input.txt', 'r') as inputFile:
backpacks = inputFile.readlines() backpacks = inputFile.readlines()
# Group the backpacks up into threes and find the common item in each three.
currentBackpack = 0 currentBackpack = 0
while(currentBackpack < len(backpacks)): while(currentBackpack < len(backpacks)):
currentGroup = [] currentGroup = []
while(len(currentGroup) < 3): while(len(currentGroup) < 3):
@@ -20,12 +22,13 @@ while(currentBackpack < len(backpacks)):
commonItems.append(item) commonItems.append(item)
break break
# For all of the common items we found, total up the priorities.
totalPriority = 0 totalPriority = 0
for item in commonItems: for item in commonItems:
if(item in ascii_lowercase): if(item in ascii_lowercase):
totalPriority += (ascii_lowercase.index(item) + 1) totalPriority += (ascii_lowercase.index(item) + 1)
elif(item in ascii_uppercase): elif(item in ascii_uppercase):
totalPriority += (ascii_uppercase.index(item) + 27) totalPriority += (ascii_uppercase.index(item) + 27)
# Print the answer.
print(totalPriority) print(totalPriority)