-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday03.py
executable file
·151 lines (117 loc) · 2.35 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
from utils.all import *
advent.setup(2021, 3)
if 'debug' not in map(str.lower, sys.argv):
fin = advent.get_input()
else:
fin = io.StringIO('''\
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
''')
# eprint(*fin, sep='', end='----- end of input -----\n\n'); fin.seek(0, 0)
ans = 0
try: lines = get_lines(fin); fin.seek(0, 0)
except: pass
occur = defaultdict(int)
for i, l in enumerate(lines):
bits = list(map(int, l.strip()))
print(bits)
for col, bit in enumerate(bits):
occur[col, bit] += 1
mostcom = {}
leastcom = {}
for col in range(len(bits)):
if occur[col, 1] >= occur[col, 0]:
mostcom[col] = 1
leastcom[col] = 0
else:
mostcom[col] = 0
leastcom[col] = 1
gamma = 0
eps = 0
for col in range(len(bits)):
gamma = gamma << 1
eps = eps << 1
gamma += mostcom[col]
eps += leastcom[col]
print(gamma, eps)
for r, row in enumerate(mat):
for c, cell in enumerate(row):
pass
ans = gamma * eps
advent.print_answer(1, ans)
# wait('Submit? ')
# advent.submit_answer(1, ans)
col = 0
filtered = set(map(lambda x: tuple(map(int, x)), lines))
while len(filtered) != 1:
todel = set()
for n in filtered:
if n[col] != mostcom[col]:
todel.add(n)
filtered -= todel
a = filtered.pop()
nbits = len(a)
filtered.add(a)
occur = defaultdict(int)
for bits in filtered:
for c, bit in enumerate(bits):
occur[c, bit] += 1
mostcom = {}
leastcom = {}
for c in range(nbits):
if occur[c, 1] >= occur[c, 0]:
mostcom[c] = 1
leastcom[c] = 0
else:
mostcom[c] = 0
leastcom[c] = 1
col += 1
if len(filtered) == 0:
break
print(filtered)
x = int(''.join(map(str, filtered.pop())), 2)
print('>>', x)
col = 0
filtered = set(map(lambda x: tuple(map(int, x)), lines))
while len(filtered) != 1:
todel = set()
for n in filtered:
if n[col] != leastcom[col]:
todel.add(n)
filtered -= todel
a = filtered.pop()
nbits = len(a)
filtered.add(a)
occur = defaultdict(int)
for bits in filtered:
for c, bit in enumerate(bits):
occur[c, bit] += 1
mostcom = {}
leastcom = {}
for c in range(nbits):
if occur[c, 1] >= occur[c, 0]:
mostcom[c] = 1
leastcom[c] = 0
else:
mostcom[c] = 0
leastcom[c] = 1
col += 1
if len(filtered) == 0:
break
y = int(''.join(map(str, filtered.pop())), 2)
print('>>', y)
ans = x * y
advent.print_answer(2, ans)
# wait('Submit? ')
advent.submit_answer(2, ans)