-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
63 lines (47 loc) · 1.51 KB
/
day3.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
59
60
61
62
63
with open("./input3.txt") as file:
report = [line.strip() for line in file]
def power_consumption(report):
counts = [0] * len(report[0])
for number in report:
for index, bit in enumerate(number):
if bit == "1":
counts[index] += 1
threshold = len(report) / 2
gamma = ""
epsilon = ""
for count in counts:
if count > threshold:
gamma += "1"
epsilon += "0"
else:
gamma += "0"
epsilon += "1"
return int(gamma, 2), int(epsilon, 2)
def life_support_rating(report):
oxygen = gas_rating(report, "1")
co2 = gas_rating(report, "0")
return oxygen, co2
def gas_rating(report, to_keep, index=0):
num_reports = len(report)
if num_reports == 1:
return int(report[0], 2)
count = 0
for number in report:
if number[index] == "1":
count += 1
if count >= (num_reports / 2):
keep = to_keep
else:
keep = "0" if to_keep == "1" else "1"
return gas_rating(
[number for number in report if number[index] == keep], to_keep, index + 1
)
if __name__ == "__main__":
gamma, epsilon = power_consumption(report)
print(
f"Part 1\nGamma rate: {gamma:b} {gamma}, Epsilon rate: {epsilon:b} {epsilon}\nPower consumption: {gamma * epsilon}"
)
oxygen, co2 = life_support_rating(report)
print(
f"Part 2\nOxygen rating: {oxygen:b} {oxygen}, CO2 rating: {co2:b} {co2}\nLife support rating: {oxygen * co2}"
)