-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (54 loc) · 1.42 KB
/
main.py
File metadata and controls
69 lines (54 loc) · 1.42 KB
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
59
60
61
62
63
64
65
66
67
68
69
from collections import Counter
def part1(test_input: str) -> int:
ll = test_input.strip().splitlines()
gamma_rate = ""
epsilon_rate = ""
for i in range(len(ll[0])):
x = ""
for j in ll:
x += j[i]
gamma_rate += Counter(x).most_common()[0][0]
epsilon_rate += Counter(x).most_common()[1][0]
result = int(gamma_rate, 2) * int(epsilon_rate, 2)
return result
def part2(test_input: str) -> int:
ll = test_input.strip().splitlines()
theta = ""
epsilon = ""
for i in range(len(ll[0])):
common = Counter([x[i] for x in ll])
if common["0"] > common["1"]:
ll = [x for x in ll if x[i] == "0"]
else:
ll = [x for x in ll if x[i] == "1"]
theta = ll[0]
ll = test_input.strip().splitlines()
for i in range(len(ll[0])):
common = Counter([x[i] for x in ll])
if common["0"] > common["1"]:
ll = [x for x in ll if x[i] == "1"]
else:
ll = [x for x in ll if x[i] == "0"]
if ll:
epsilon = ll[0]
return int(theta, 2) * int(epsilon, 2)
if __name__ == "__main__":
test_in = """00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010"""
assert part1(test_in) == 198
f = open("../inputs/2021-03.txt", mode="r").read()
res = part1(f)
print(res)
assert part2(test_in) == 230
res = part2(f)
print(res)