Day 3: Add Python solution
This commit is contained in:
parent
91c74473a8
commit
78d929de2f
3 changed files with 1030 additions and 0 deletions
1000
03/input.txt
Normal file
1000
03/input.txt
Normal file
File diff suppressed because it is too large
Load diff
9
03/solution_1.py
Executable file
9
03/solution_1.py
Executable file
|
@ -0,0 +1,9 @@
|
|||
#!/usr/bin/env python3
|
||||
from collections import Counter
|
||||
|
||||
with open("input.txt", "r") as _input:
|
||||
most_common_bits = [Counter(col).most_common(2) for col in zip(*[l.rstrip() for l in _input.readlines()][::-1])]
|
||||
|
||||
gamma, epsilon = [int("".join(x)[::-1], 2) for x in zip(*[(x[0], y[0]) for x, y in most_common_bits][::-1])]
|
||||
|
||||
print("Answer:", gamma * epsilon)
|
21
03/solution_2.py
Executable file
21
03/solution_2.py
Executable file
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python3
|
||||
from collections import Counter
|
||||
|
||||
def flt(data, most):
|
||||
positions = len(data[0])
|
||||
for i in range(positions):
|
||||
cnt = Counter(list(zip(*data[::-1]))[i]).most_common(2)
|
||||
if len(cnt) > 1:
|
||||
if cnt[0][1] == cnt[1][1]:
|
||||
cond = '1' if most else '0'
|
||||
else:
|
||||
cond = cnt[0 if most else 1][0]
|
||||
data = [x for x in data if x[i] == cond]
|
||||
if len(data) == 1:
|
||||
break
|
||||
return data[0]
|
||||
|
||||
with open("input.txt", "r") as _input:
|
||||
data = [l.rstrip() for l in _input.readlines()]
|
||||
|
||||
print("Answer:", int(flt(data, True), 2) * int(flt(data, False), 2))
|
Loading…
Reference in a new issue