-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_binary_diagnostic.py
58 lines (44 loc) · 1.46 KB
/
03_binary_diagnostic.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# https://adventofcode.com/2021/day/3
from utils import *
from collections import Counter
def star1():
lines = readlines('3_input.txt')
counters = [Counter(bits) for bits in zip(*lines)]
gamma, epsilon = [int(''.join(c.most_common()[pos][0] for c in counters), base=2) for pos in [0, -1]]
print(gamma * epsilon)
print('Star 1:')
star1()
def count_most_common(lines):
count_zeroes = [0] * len(lines[0])
for line in lines:
for i, c in enumerate(line):
if c == '0':
count_zeroes[i] += 1
result = [0] * len(count_zeroes)
for i, c in enumerate(count_zeroes):
is_one = c < (len(lines) / 2)
if is_one:
result[i] = 1
return result
def find_oxy(lines):
for i in range(len(lines[0])):
cnts = Counter(line[i] for line in lines)
b = '1' if cnts['1'] >= cnts['0'] else '0'
tmp = [line for line in lines if line[i] == b]
if len(tmp) == 1:
return int(''.join(tmp[0]), base=2)
lines = tmp
def find_co2(lines):
for i in range(len(lines[0])):
cnts = Counter(line[i] for line in lines)
b = '1' if cnts['1'] < cnts['0'] else '0'
tmp = [line for line in lines if line[i] == b]
if len(tmp) == 1:
return int(''.join(tmp[0]), base=2)
lines = tmp
def star2():
lines = readlines('3_input.txt')
oxy, co2 = find_oxy(lines), find_co2(lines)
print(oxy, co2)
print(oxy * co2)
star2()