Benjamin Kraft 3 years ago
parent 44e89f9e62
commit 2cffff0ca9
  1. 57
      03/03.py
  2. 1000
      03/input
  3. 12
      03/testInput

@ -0,0 +1,57 @@
with open("input", "r") as file:
data = file.read().splitlines()
def binaryToDecimal(binArray):
base = 2
result = 0
for position in range(len(binArray)):
exp = len(binArray) - position - 1
result += binArray[position] * (base ** exp)
return result
# Task 1
sums = [0 for _ in range(len(data[0]))]
for line in data:
for i in range(len(line)):
sums[i] += int(line[i])
gammaBin, epsilonBin = [], []
for s in sums:
if s < len(data) // 2:
gammaBin.append(0)
epsilonBin.append(1)
if s > len(data) // 2:
gammaBin.append(1)
epsilonBin.append(0)
gamma, epsilon = binaryToDecimal(gammaBin), binaryToDecimal(epsilonBin)
print(gamma, epsilon)
print(gamma * epsilon)
# Task 2
def findRating(currentData, pos=0, least=False):
if len(currentData) == 1:
return [int(element) for element in currentData[0]]
oneRows, zeroRows = [], []
for binaryString in currentData:
bit = int(binaryString[pos])
if bit == 0:
zeroRows.append(binaryString)
if bit == 1:
oneRows.append(binaryString)
if least:
newData = zeroRows if len(zeroRows) <= len(oneRows) else oneRows
else:
newData = oneRows if len(oneRows) >= len(zeroRows) else zeroRows
return findRating(newData, pos + 1, least)
oxygenGenerator = binaryToDecimal(findRating(data.copy()))
co2Scrubber = binaryToDecimal(findRating(data.copy(), least=True))
print(oxygenGenerator, co2Scrubber)
print(oxygenGenerator * co2Scrubber)

1000
03/input

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
Loading…
Cancel
Save