-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_03.py
65 lines (46 loc) · 1.4 KB
/
day_03.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
with open("./day_03_input.txt", "rt") as f:
nums_str = f.read()
nums = [int(num.strip(), 2) for num in nums_str.split("\n") if num.strip()]
def problem_one():
gamma = 0
epsilon = 0
for i in range(12):
bits = list(map(lambda x: (x >> i) & 1, nums))
ones = len([x for x in bits if x == 1])
zeros = 1000 - ones
assert ones != zeros
if ones > zeros:
x = 1
y = 0
else:
x = 0
y = 1
gamma += x << i
epsilon += y << i
return gamma * epsilon
def problem_two():
o2_nums = nums
for i in range(11, -1, -1):
bits = list(map(lambda x: (x >> i) & 1, o2_nums))
ones = len([x for x in bits if x == 1])
zeros = len(bits) - ones
if ones >= zeros:
x = 1
else:
x = 0
o2_nums = [o2_num for o2_num in o2_nums if (o2_num >> i) & 1 == x]
if len(o2_nums) == 1:
break
co2_nums = nums
for i in range(11, -1, -1):
bits = list(map(lambda x: (x >> i) & 1, co2_nums))
ones = len([x for x in bits if x == 1])
zeros = len(bits) - ones
if ones >= zeros:
x = 0
else:
x = 1
co2_nums = [co2_num for co2_num in co2_nums if (co2_num >> i) & 1 == x]
if len(co2_nums) == 1:
break
return o2_nums[0] * co2_nums[0]