blob: f969550264d1b5f52f6293746fb2921095280674 (
plain) (
tree)
|
|
#!/usr/bin/env python
import numpy as np
with open("day3.txt") as data:
counts = np.array([0] * 12)
for i, line in enumerate(data):
line = np.array([num for num in map(int, list(line.strip()))])
counts += line
gamma = (counts > i/2).astype(int).tolist()
epsilon = (counts < i/2).astype(int).tolist()
gamma = int("".join(str(x) for x in gamma), 2)
epsilon = int("".join(str(x) for x in epsilon), 2)
print(gamma * epsilon)
# part 2
with open("day3.txt") as data:
# load data into numpy array
inputs = np.zeros((1000, 12)).astype(int)
for i, line in enumerate(data):
line = np.array([num for num in map(int, list(line.strip()))])
inputs[i] = line
oxygen = inputs.copy()
for i in range(12):
counts = np.bincount(oxygen[:, i])
if counts[0] == counts[1]:
bit = 1
else:
bit = np.argmax(counts)
oxygen = oxygen[oxygen[:, i] == bit]
oxygen = int("".join(str(x) for x in oxygen[0].tolist()), 2)
co2 = inputs.copy()
for i in range(12):
counts = np.bincount(co2[:, i])
print(counts)
if len(counts) < 2 or counts[0] == counts[1]:
bit = 0
else:
bit = np.argmin(counts)
bit = np.argmin(np.bincount(co2[:, i]))
co2 = co2[co2[:, i] == bit]
if len(counts) < 2:
break
co2 = int("".join(str(x) for x in co2[0].tolist()), 2)
print(oxygen * co2)
|