Again, I will be using helper function from part 1 to read my data.
Part 1
from collections import Counter
import numpy as np
data,data1 = get_data(day=3)
def part1(inp):
cs = len(inp[0])
dt = [int(d) for dt in inp for d in dt]
dt = np.array(dt).reshape(-1, cs)
print(dt[0])
# sorted(x.items(), key=lambda item: item[1])
counts = [sorted(dict(Counter(dt[:, i])).items(), key=lambda item: item[1]) for i in range(len(dt[0]))]
counts = np.array(counts).reshape(-1,2)
# print(counts)
minidx = np.arange(0, len(counts), 2)
maxidx = np.arange(1, len(counts), 2)
minv = int("".join(list(map(str, counts[minidx, 0]))), 2)
maxv = int("".join(list(map(str, counts[maxidx, 0]))), 2)
print(minv, maxv)
print(minv*maxv)
part1(data)
The output of above code will be:
[0 0 1 0 0]
9 22
198
198 is a test output. The real output will be 4103154.
Part 2
def o2(dt):
ndt = dt.copy()
#print(len(ndt[0]))
curr_c = 0
while curr_c<len(dt[0]):
print(f"Current Col: {curr_c}")
counts = [sorted(dict(Counter(ndt[:, curr_c])).items(), key=lambda item: item[1])]
counts = np.array(counts).reshape(-1,2)
if len(counts)>1:
if counts[0, 1]==counts[1, 1]:
ndt = ndt[ndt[:,curr_c]==1]
else:
ndt = ndt[ndt[:,curr_c]==counts[1][0]]
else:
ndt = ndt[ndt[:,curr_c]==counts[1][0]]
print(f"Current Col: {curr_c} Rows: {len(ndt)}")
# print(counts)
# print(ndt[ndt[:,curr_c]==counts[1][0]])
curr_c+=1
res = int("".join(list(map(str, ndt[0]))), 2)
print(res)
return res
def co2(dt):
ndt = dt.copy()
#print(len(ndt[0]))
curr_c = 0
while curr_c<len(dt[0]):
counts = [sorted(dict(Counter(ndt[:, curr_c])).items(), key=lambda item: item[1])]
counts = np.array(counts).reshape(-1,2)
if len(counts)>1:
if counts[0, 1]== counts[1, 1]:
ndt = ndt[ndt[:,curr_c]==0]
else:
ndt = ndt[ndt[:,curr_c]==counts[0][0]]
else:
ndt = ndt[ndt[:,curr_c]==counts[0][0]]
# print(ndt)
print(f"Current Col: {curr_c} Rows: {len(ndt)}")
curr_c+=1
return int("".join(list(map(str, ndt[0]))), 2)
def part2(inp):
cs = len(inp[0])
dt = [int(d) for dt in inp for d in dt]
dt = np.array(dt).reshape(-1, cs)
print("O2")
o2v = o2(dt)
co2v = co2(dt)
print(o2v, co2v)
print(o2v*co2v)
part2(data1)
The output will be:
O2
Current Col: 0
Current Col: 0 Rows: 511
Current Col: 1
Current Col: 1 Rows: 263
Current Col: 2
Current Col: 2 Rows: 134
Current Col: 3
Current Col: 3 Rows: 71
Current Col: 4
Current Col: 4 Rows: 36
Current Col: 5
Current Col: 5 Rows: 20
Current Col: 6
Current Col: 6 Rows: 12
Current Col: 7
Current Col: 7 Rows: 8
Current Col: 8
Current Col: 8 Rows: 5
Current Col: 9
Current Col: 9 Rows: 3
Current Col: 10
Current Col: 10 Rows: 2
Current Col: 11
Current Col: 11 Rows: 1
3399
Current Col: 0 Rows: 489
Current Col: 1 Rows: 235
Current Col: 2 Rows: 109
Current Col: 3 Rows: 54
Current Col: 4 Rows: 24
Current Col: 5 Rows: 11
Current Col: 6 Rows: 4
Current Col: 7 Rows: 2
Current Col: 8 Rows: 1
Current Col: 9 Rows: 1
Current Col: 10 Rows: 1
Current Col: 11 Rows: 1
3399 1249
4245351
All of my codes are available in GitHub as Jupyter Notebook.
Why not read more?
- Gesture Based Visually Writing System Using OpenCV and Python
- Gesture Based Visually Writing System: Adding Visual User Interface
- Gesture Based Visually Writing System: Adding Virtual Animationn, New Mode and New VUI
- Gesture Based Visually Writing System: Add Slider, More Colors and Optimized OOP code
- Gesture Based Visually Writing System: A Web App
- Contour Based Game: Break The Bricks
- Linear Regression from Scratch
- Writing Popular ML Optimizers from Scratch
- Feed Forward Neural Network from Scratch
- Convolutional Neural Networks from Scratch
- Writing a Simple Image Processing Class from Scratch
- Deploying a RASA Chatbot on Android using Unity3d
- Naive Bayes for text classifications: Scratch to Framework
- Simple OCR for Devanagari Handwritten Text
Top comments (0)