Repository navigation
Expand file tree
/
Copy pathday03.py
More file actions
executable file
·81 lines (70 loc) · 1.75 KB
/
Copy pathday03.py
File metadata and controls
executable file
·81 lines (70 loc) · 1.75 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
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
day3_test_input = [
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010",
]
def power(inputs):
γ = ""
Ɛ = ""
for i in range(len(inputs[0])):
ones = 0
zeros = 0
for s in inputs:
if s[i] == '1':
ones += 1
else:
zeros += 1
if ones == zeros:
raise Exception('same number of ones as zeros, bit %d, %d==%d', i, ones, zeros)
if ones > zeros:
γ += '1'
Ɛ += '0'
else:
γ += '0'
Ɛ += '1'
return int(γ,2) * int(Ɛ,2)
with open("inputs/day03.input") as f:
day3_input = f.read().splitlines()
day3a = power(day3_input)
print("Day 3 Part 1 =>", day3a) # => 3985686
#### Part 2
def popularity_split(lst, digit):
"""Split lst into two lists on digit-th digit, more popular first"""
ones, zeros = [], []
for s in lst:
if s[digit] == '1':
ones.append(s)
else:
zeros.append(s)
if len(zeros) > len(ones):
return zeros, ones
return ones, zeros
def oxygen(lst):
i = 0
while len(lst) > 1 and i < len(lst[0]):
lst, _ = popularity_split(lst, i)
i += 1
return int(lst[0], 2)
def co2(lst):
i = 0
while len(lst) > 1 and i < len(lst[0]):
_, lst = popularity_split(lst, i)
i += 1
return int(lst[0], 2)
def life_support(lst):
return oxygen(lst) * co2(lst)
got = life_support(day3_test_input)
if got != 230:
raise Exception('life_support on test input got %d want %d' % (got, 230))
day3b = life_support(day3_input)
print("Day 3 Part 2 =>", day3b) # => 2555739