day 3
This commit is contained in:
parent
c898811c9f
commit
09c269c9f2
33
3/a.py
Normal file
33
3/a.py
Normal file
@ -0,0 +1,33 @@
|
||||
with open("input.txt") as f:
|
||||
lines = [i.strip() for i in f.readlines()]
|
||||
|
||||
|
||||
def count_digits(ar):
|
||||
zeros = [0] * len(ar[0])
|
||||
ones = [0] * len(ar[0])
|
||||
|
||||
for row in ar:
|
||||
for pos, c in enumerate(row.strip()):
|
||||
(zeros if c == "0" else ones)[pos] += 1
|
||||
|
||||
return zeros, ones
|
||||
|
||||
|
||||
zeros, ones = count_digits(lines)
|
||||
|
||||
|
||||
print(zeros)
|
||||
print(ones)
|
||||
|
||||
# lol
|
||||
gamma_bits = []
|
||||
epsi_bits = []
|
||||
|
||||
for pos, zero in enumerate(zeros):
|
||||
gamma_bits.append("1" if zero < ones[pos] else "0")
|
||||
epsi_bits.append("1" if zero > ones[pos] else "0")
|
||||
|
||||
gamma = int("".join(gamma_bits), 2)
|
||||
epsi = int("".join(epsi_bits), 2)
|
||||
|
||||
print(gamma * epsi)
|
56
3/b.py
Normal file
56
3/b.py
Normal file
@ -0,0 +1,56 @@
|
||||
with open("input.txt") as f:
|
||||
og_lines = [i.strip() for i in f.readlines()]
|
||||
|
||||
lines = og_lines[:]
|
||||
|
||||
|
||||
def count_digits(ar):
|
||||
zeros = [0] * len(ar[0])
|
||||
ones = [0] * len(ar[0])
|
||||
|
||||
for row in ar:
|
||||
for pos, c in enumerate(row.strip()):
|
||||
(zeros if c == "0" else ones)[pos] += 1
|
||||
|
||||
return zeros, ones
|
||||
|
||||
|
||||
def filter_by_digit(lines, position, value):
|
||||
return [i for i in lines if i[position] == value]
|
||||
|
||||
|
||||
# oxy = most common
|
||||
# co2 = least common
|
||||
|
||||
# oxy
|
||||
position = 0
|
||||
while len(lines) > 1:
|
||||
zeros, ones = count_digits(lines)
|
||||
zeros, ones = zeros[position], ones[position]
|
||||
|
||||
print(zeros, ones, "pos", position, "remaining lines", len(lines))
|
||||
lines = filter_by_digit(lines, position, "1" if ones >= zeros else "0")
|
||||
|
||||
position += 1
|
||||
|
||||
oxy = lines[0]
|
||||
|
||||
|
||||
lines = og_lines[:]
|
||||
|
||||
# co0
|
||||
position = 0
|
||||
while len(lines) > 1:
|
||||
zeros, ones = count_digits(lines)
|
||||
zeros, ones = zeros[position], ones[position]
|
||||
|
||||
print(zeros, ones, "pos", position, "remaining lines", len(lines))
|
||||
lines = filter_by_digit(lines, position, "0" if zeros <= ones else "1")
|
||||
|
||||
position += 1
|
||||
|
||||
co2 = lines[0]
|
||||
|
||||
print(int(oxy, 2) * int(co2, 2))
|
||||
|
||||
# 29m
|
1000
3/input.txt
Normal file
1000
3/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user