-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.py
77 lines (60 loc) · 2.78 KB
/
day03.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
from collections import Counter
from utils import transpose
with open("day03-input.txt", "r") as f:
data = f.readlines()
def formatData(data):
return [[int(y) for y in word.strip()] for word in data]
def mostCommonBitNumber(data):
# format data into lists of integers for later handling
intLists = formatData(data)
# transpose
charAtIndexCollections = transpose(intLists)
# count most common in each list
mostCommons = [
Counter(group).most_common(1)[0][0] for group in charAtIndexCollections
]
# convert to result format
binaryString = "".join(str(num) for num in mostCommons)
return binaryString
def leastCommonBitNumber(data):
mostCommonsBinary = mostCommonBitNumber(data)
intList = [int(y) for y in mostCommonsBinary]
leastCommons = [1 if x == 0 else 0 for x in intList]
# convert to result format
binaryString = "".join(str(num) for num in leastCommons)
return binaryString
def powerConsumption(data):
return int(mostCommonBitNumber(data), 2) * int(leastCommonBitNumber(data), 2)
# To find oxygen generator rating, determine the most common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 1 in the position being considered.
def o2GeneratorRating(data) -> str:
index = 0
ifEqualCountsPerfer = 1
while len(data) > 1:
charIndexGroups = transpose(formatData(data))
mostCommon, countMostCommon = Counter(charIndexGroups[index]).most_common(1)[0]
# Case: check against equal count
if countMostCommon == len(charIndexGroups[index]) / 2:
mostCommon = ifEqualCountsPerfer
data = [x for x in data if int(x[index]) == mostCommon]
index += 1
return data[0].strip()
# To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, keep values with a 0 in the position being considered.
def co2ScrubberRating(data) -> str:
index = 0
ifEqualCountsPerfer = 0
while len(data) > 1:
charIndexGroups = transpose(formatData(data))
leastCommon, countLeastCommon = Counter(charIndexGroups[index]).most_common()[
-1
]
# Case: check against equal count
if countLeastCommon == len(charIndexGroups[index]) / 2:
leastCommon = ifEqualCountsPerfer
data = [x for x in data if int(x[index]) == leastCommon]
index += 1
return data[0].strip()
def lifeSupportRating(data) -> int:
return int(o2GeneratorRating(data), 2) * int(co2ScrubberRating(data), 2)
if __name__ == "__main__":
print(f"Day03a: {powerConsumption(data)}")
print(f"Day03b: {lifeSupportRating(data)}")