-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
44 lines (38 loc) · 1.17 KB
/
3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import helper
import numpy as np
from statistics import multimode
def task1():
ns = helper.get_int_input_for_file("3")
l = len(ns)
binlist = np.array([toDigits(n) for n in ns])
sums = binlist.transpose().sum(1).tolist()
g = toInt([1 if a > (l // 2) else 0 for a in sums])
h = toInt([1 if a < (l // 2) else 0 for a in sums])
print(g*h)
def task2():
ns = helper.get_int_input_for_file("3")
binmatrix = np.array([toDigits(n) for n in ns])
o2Rating = calculateRating(binmatrix.copy())
co2Rating = calculateRating(binmatrix.copy(), 1)
print(o2Rating*co2Rating)
# overengineered function
def calculateRating(matrix, thing=0):
for i in range(12):
temp = matrix[:,i]
# if cnt(1) == cnt(0): m = 1
m = sorted(multimode(temp), reverse=True)[0]
if thing: # reverse value for co2Rating
m = 1 - m
matrix = matrix[temp == m] # filter stuff
if len(matrix) == 1:
return toInt(matrix[0])
def toDigits(d:int):
return np.array([(d // 10**(11-i)) % 10 for i in range(12)])
def toInt(d):
b:int = 0
for i in range(12):
b += (d[i])*2**(11-i)
return b
if __name__ == '__main__':
task1()
task2()