-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.py
81 lines (64 loc) · 2.13 KB
/
3.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
INPUT_FILE = f"input/{__file__.split('.')[0].rstrip('b')}"
def getInput():
with open(INPUT_FILE, 'r') as f:
return f.read().splitlines()
def partOne():
inp = getInput()
count = len(inp)
width = len(inp[0])
ones = zeros = row = col = 0
gamma = epsilon = ""
while col < width:
while row < count:
bit = int(inp[row][col])
if bit == 0:
zeros += 1
else:
ones += 1
row += 1
gamma += "1" if ones > zeros else "0"
epsilon += "1" if zeros > ones else "0"
row = zeros = ones = 0
col += 1
gammaDec = int(gamma, 2)
epsilonDec = int(epsilon, 2)
return gammaDec * epsilonDec
def partTwo():
inp = getInput()
oxygen = processList(inp, True)
co2 = processList(inp, False)
return oxygen * co2
def getNumbers(lines, bit, pos, includeOnes, includeZeros):
numbers = []
for line in lines:
if (includeOnes == True and int(line[pos]) == 1) or (includeZeros == True and int(line[pos]) == 0) or (includeOnes == False and includeZeros == False and int(line[pos]) == int(bit)):
numbers.append(line)
return numbers
def processList(lines, commonOnly):
count = len(lines)
width = len(lines[0])
ones = zeros = row = col = 0
while col < width:
while row < count:
bit = int(lines[row][col])
if bit == 0:
zeros += 1
else:
ones += 1
row += 1
common = 1 if ones > zeros else 0
notCommon = 1 if zeros > ones else 0
lines = getNumbers(lines, common if commonOnly else notCommon, col, ones == zeros and commonOnly, ones == zeros and not commonOnly)
if len(lines) > 1:
zeros = ones = row = 0
col += 1
else:
break
count = len(lines)
width = len(lines[0])
return int(lines[0], 2)
if __name__ == "__main__":
one = partOne()
two = partTwo()
print(f"Part one: {one}")
print(f"Part two: {two}")