Fix directories so order makes sense

This commit is contained in:
2022-11-30 21:05:44 +00:00
parent 81e4a1ed1a
commit af978e71b5
17 changed files with 0 additions and 0 deletions

33
2021/Day-03/day3-1.py Normal file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/python3
# Variables to hold our totals.
gamma = ''
epsilon = ''
# Read in the input file. Split the file up into an array.
values = []
with open('input.txt', 'r') as inputFile:
for line in inputFile:
values.append(line.strip('\n'))
# Add up all the 1 values for each bit, then use the length of the array to calculate the most common bit.
for i in range(0, len(values[0])):
currentBitCount = 0
for value in values:
currentBitCount = currentBitCount + int(value[i])
if((len(values) - currentBitCount) < currentBitCount):
gamma = gamma + '1'
epsilon = epsilon + '0'
elif((len(values) - currentBitCount) > currentBitCount):
gamma = gamma + '0'
epsilon = epsilon + '1'
# Convert the binary strings to decimal ints.
gammaInt = int(gamma, 2)
epsilonInt = int(epsilon, 2)
# Print results.
print("Gamma is " + str(gamma) + " (" + str(gammaInt) + ")")
print("Epsilon is " + str(epsilon) + " (" + str(epsilonInt) + ")")
print("Gamma multiplied by Epsilon is: " + str(gammaInt * epsilonInt))

67
2021/Day-03/day3-2.py Normal file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/python3
# Variables to hold our totals.
keptNumbers = []
co2 = 0
o2 = 0
# Read in the input file. Split the file up into an array.
rawValues = []
with open('input.txt', 'r') as inputFile:
for line in inputFile:
rawValues.append(line.strip('\n'))
# Loop through twice, collecting the opposite set of results each time.
for i in range(0,2):
values = rawValues
mostPopular=0
# Determine the most popular bit.
for bit in range(0, len(values[0])):
currentBitCount = 0
for value in values:
currentBitCount = currentBitCount + int(value[bit])
# Keep values which contain the most popular bit.
if((len(values) - currentBitCount) < currentBitCount):
mostPopular = 1
for value in values:
if(value[bit] == str(mostPopular) and i == 1):
keptNumbers.append(value)
elif(value[bit] != str(mostPopular) and i == 0):
keptNumbers.append(value)
elif((len(values) - currentBitCount) > currentBitCount):
mostPopular = 0
for value in values:
if(value[bit] == str(mostPopular) and i == 1):
keptNumbers.append(value)
elif(value[bit] != str(mostPopular) and i == 0):
keptNumbers.append(value)
elif((len(values) - currentBitCount) == currentBitCount):
for value in values:
if(value[bit] == str(i)):
keptNumbers.append(value)
values = keptNumbers
keptNumbers = []
# Break if we only have one value left.
if(len(values) == 1):
break
# Put the value in the right variable.
if(i == 0):
co2 = values[0]
elif(i == 1):
o2 = values[0]
# Convert the binary numbers to ints.
o2Int = int(o2, 2)
co2Int = int(co2, 2)
# Print results.
print("O2 Generator reading is " + str(o2) + " (" + str(o2Int) + ")")
print("CO2 Scrubber reading is " + str(co2) + " (" + str(co2Int) + ")")
print("The readings multipied together is " + str(o2Int * co2Int))