-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.py
More file actions
72 lines (50 loc) · 1.35 KB
/
day03.py
File metadata and controls
72 lines (50 loc) · 1.35 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
65
66
67
68
69
70
71
72
def parse(input):
return input.strip().splitlines()
def part1(input):
input = parse(input)
gamma = 0
epsilon = 0
bit_size = len(input[0])
rows = len(input)
for pos in range(bit_size):
tot = 0
for row in range(len(input)):
tot += int(input[row][pos])
shift = bit_size - pos - 1
g = round(tot/rows)
gamma |= g << shift
epsilon |= (1 - g) << shift
return gamma * epsilon
def part2(input):
input = parse(input)
bit_size = len(input[0])
values = [int(v, 2) for v in input]
om = 0b0
cm = 0b0
fm = 0b0
oxy = None
co2 = None
for pos in range(bit_size-1, -1, -1):
oc = 0
cc = 0
for value in values:
if ((value ^ om) & fm) == 0:
oc += 1 if value & (1 << pos) else -1
oxy = value
if ((value ^ cm) & fm) == 0:
cc += 1 if value & (1 << pos) else -1
co2 = value
if oc >= 0:
om |= 1 << pos
if cc < 0:
cm |= 1 << pos
fm |= 1 << pos
return oxy * co2
if __name__ == '__main__':
import sys
import utils
input = sys.stdin.read()
p1, t1 = utils.time(part1, input)
print(f'part1: {p1} ({t1:.20f})')
p2, t2 = utils.time(part2, input)
print(f'part2: {p2} ({t2:.20f})')