2021/3 challenge
This commit is contained in:
parent
c29460a0a9
commit
672d8df7b6
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
file = [i.rstrip() for i in open("input").readlines()]
|
||||
|
||||
def gammaepsil(data):
|
||||
bufg = []
|
||||
bufe = []
|
||||
for i in range(len(data[0])):
|
||||
one = 0
|
||||
zero = 0
|
||||
for j in range(len(data)):
|
||||
if data[j][i] == "0":
|
||||
zero += 1
|
||||
else:
|
||||
one += 1
|
||||
if zero > one:
|
||||
bufg.append("0")
|
||||
bufe.append("1")
|
||||
else:
|
||||
bufg.append("1")
|
||||
bufe.append("0")
|
||||
return [int(''.join(bufg), base=2), int(''.join(bufe), base=2)]
|
||||
|
||||
|
||||
res = gammaepsil(file)
|
||||
print(res[0], res[1], res[0]*res[1])
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
files = [i.rstrip() for i in open("input").readlines()]
|
||||
|
||||
def oxy(bits):
|
||||
zero = 0
|
||||
one = 0
|
||||
for i in bits:
|
||||
if i == "0":
|
||||
zero += 1
|
||||
else:
|
||||
one += 1
|
||||
|
||||
if zero > one:
|
||||
return 0
|
||||
elif one > zero:
|
||||
return 1
|
||||
elif one == zero:
|
||||
return 1
|
||||
|
||||
def co2(bits):
|
||||
zero = 0
|
||||
one = 0
|
||||
for i in bits:
|
||||
if i == "0":
|
||||
zero += 1
|
||||
else:
|
||||
one += 1
|
||||
|
||||
if zero < one:
|
||||
return 0
|
||||
elif one < zero:
|
||||
return 1
|
||||
elif one == zero:
|
||||
return 0
|
||||
|
||||
def oxygen(fields):
|
||||
buf = fields
|
||||
for i in range(len(fields[0])):
|
||||
testing = [j[i] for j in buf]
|
||||
oxygenrating = oxy(testing)
|
||||
buf = [j for j in buf if int(j[i]) == oxygenrating]
|
||||
|
||||
return buf
|
||||
|
||||
def carbdio(fields):
|
||||
buf = fields
|
||||
for i in range(len(fields[0])):
|
||||
if len(buf) == 1: break
|
||||
testing = [j[i] for j in buf]
|
||||
print(buf, testing)
|
||||
co2rating = co2(testing)
|
||||
buf = [j for j in buf if int(j[i]) == co2rating]
|
||||
|
||||
return buf
|
||||
|
||||
oxygenreal = int(''.join(oxygen(files)), base=2)
|
||||
carbdioreal = int(''.join(carbdio(files)), base=2)
|
||||
|
||||
print(oxygenreal, carbdioreal, oxygenreal * carbdioreal)
|
|
@ -0,0 +1,12 @@
|
|||
00100
|
||||
11110
|
||||
10110
|
||||
10111
|
||||
10101
|
||||
01111
|
||||
00111
|
||||
11100
|
||||
10000
|
||||
11001
|
||||
00010
|
||||
01010
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue