-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03.py
More file actions
64 lines (48 loc) · 1.56 KB
/
Day03.py
File metadata and controls
64 lines (48 loc) · 1.56 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
import Utility
filePath = 'input/day03/part1.txt'
def findMostCommonBit(inputLines, index):
total = sum(map(lambda x: int(x[index]), inputLines))
if (total * 2 >= len(inputLines)):
return '1'
else:
return '0'
def solvePart1():
inputLines = Utility.getLinesFromFile(filePath)
gamma, epsilon = 0, 0
mostCommonBits = ''
leastCommonBits = ''
for i in range(len(inputLines[0])):
if findMostCommonBit(inputLines, i) == '1':
mostCommonBits += '1'
leastCommonBits += '0'
else:
mostCommonBits += '0'
leastCommonBits += '1'
gamma = int(mostCommonBits, 2)
epsilon = int(leastCommonBits, 2)
print('Solution to part1:')
print(gamma * epsilon)
def solvePart2():
inputLines = Utility.getLinesFromFile(filePath)
oxygen, co2 = 0, 0
oxygenLines = inputLines.copy()
co2Lines = inputLines.copy()
oxygenIndex = 0
while(len(oxygenLines) > 1):
mostCommonBit = findMostCommonBit(oxygenLines, oxygenIndex)
oxygenLines = list(
filter(lambda n: n[oxygenIndex] == mostCommonBit, oxygenLines))
oxygenIndex += 1
co2Index = 0
while(len(co2Lines) > 1):
mostCommonBit = findMostCommonBit(co2Lines, co2Index)
co2Lines = list(
filter(lambda n: n[co2Index] != mostCommonBit, co2Lines))
co2Index += 1
oxygen = int(oxygenLines[0], 2)
co2 = int(co2Lines[0], 2)
print('Solution to part2:')
print(oxygen * co2)
if(__name__ == '__main__'):
solvePart1()
solvePart2()