-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.py
More file actions
32 lines (26 loc) · 664 Bytes
/
Copy pathday6.py
File metadata and controls
32 lines (26 loc) · 664 Bytes
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
from collections import defaultdict
DATA = open('day6.txt').read()
def run(n=4):
counts = defaultdict(int)
num_unique = 0
for i, c in enumerate(DATA):
if counts[c] == 1:
num_unique -= 1
elif counts[c] == 0:
num_unique += 1
counts[c] += 1
if i >= n:
d = DATA[i - n]
if counts[d] == 1:
num_unique -= 1
elif counts[d] == 2:
num_unique += 1
counts[d] -= 1
if num_unique == n:
return i + 1
result = run(4)
print(result)
assert result == 1361
result = run(14)
print(result)
assert result == 3263