-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
52 lines (37 loc) · 1.15 KB
/
day6.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
# day 6
import collections
testlines = ['mjqjpqmgbljsphdztnvjfqwrcgsmlb',
'bvwbjplbgvbhsrlpgdmjqwftvncz',
'nppdvjthqldpwncqszvftbrmjlhg',
'nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg',
'zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw']
testpos = (7, 5, 6, 10, 11)
with open('day6.txt') as fp:
data = fp.read()
def find_start_of_packet(line, bufsize):
d = collections.deque(maxlen=bufsize)
for i, c in enumerate(line):
d.append(c)
if bufsize == len(set(d)):
return i, d
def part1(line):
bufsize = 4
i, d = find_start_of_packet(line, bufsize)
return i
if __name__ == '__main__':
print('Part 1')
for i, line in enumerate(testlines):
assert testpos[i] == part1(line) + 1
print('All test lines passed')
print('Data: ', part1(data) + 1)
testpos = (19, 23, 23, 29, 26)
def part2(line):
bufsize = 14
i, d = find_start_of_packet(line, bufsize)
return i
if __name__ == '__main__':
print('Part 2')
for i, line in enumerate(testlines):
assert testpos[i] == part2(line) + 1
print('All test lines passed')
print('Data: ', part2(data) + 1)