-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
131 lines (108 loc) · 3.6 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def part1():
with open('resources/input3.txt') as f:
lines = f.readlines()
total0 = list()
total1 = list()
for line in lines:
index = 0
for char in line:
if len(total1) < index + 1:
total1.append(0)
total0.append(0)
if char == '1':
total1[index] = total1[index] + 1
else:
total0[index] = total0[index] + 1
index += 1
gamma_rate_list = list()
epsilon_rate_list = list()
for index in range(0, len(total1)-1):
if total0[index] > total1[index]:
gamma_rate_list.append("0")
epsilon_rate_list.append("1")
else:
gamma_rate_list.append("1")
epsilon_rate_list.append("0")
print(gamma_rate_list)
gamma_rate_string = "".join(gamma_rate_list)
epsilon_rate_string = "".join(epsilon_rate_list)
gamma_rate = binaryToDecimal(int(gamma_rate_string))
epsilon_rate = binaryToDecimal(int(epsilon_rate_string))
print(gamma_rate * epsilon_rate)
def binaryToDecimal(binary):
decimal, i, n = 0, 0, 0
while (binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary // 10
i += 1
return decimal
def part2():
with open('resources/input3.txt') as f:
lines = f.readlines()
oxygen_running_list = lines
co2_running_list = lines
total0 = list()
total1 = list()
entry_length = 0
for line in lines:
entry_length = len(line)
for index in range(0, entry_length):
if len(total1) < index + 1:
total1.append(0)
total0.append(0)
for line in oxygen_running_list:
char = line[index]
if char == '1':
total1[index] = total1[index] + 1
else:
total0[index] = total0[index] + 1
value = "1"
if total1[index] < total0[index]:
value = "0"
oxygen_running_list = filterValues(oxygen_running_list, index, value)
index += 1
oxygen_list = list()
for index in range(0, len(total1)):
if total0[index] <= total1[index]:
oxygen_list.append("1")
else:
oxygen_list.append("0")
oxygen_string = findEntry(oxygen_running_list, entry_length, 1)
oxygen = binaryToDecimal(int(oxygen_string))
co2_string = findEntry(co2_running_list, entry_length, 0)
co2 = binaryToDecimal(int(co2_string))
print(oxygen)
print(co2)
print(co2 * oxygen)
def filterValues(running_list, index, value):
new_list = list()
for entry in running_list:
if entry[index] == value:
new_list.append(entry)
return new_list
def findEntry(running_list, entry_length, find_larger):
for index in range(0, entry_length):
total1 = 0
total0 = 0
for line in running_list:
char = line[index]
if char == '1':
total1 += 1
else:
total0 += 1
value = "0"
if find_larger:
value = "1"
if total1 < total0:
value = "0"
else:
if total1 < total0:
value = "1"
running_list = filterValues(running_list, index, value)
index += 1
if len(running_list) == 1:
return running_list[0]
if __name__ == '__main__':
part1()
part2()