-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06.py
More file actions
executable file
·38 lines (27 loc) · 1.09 KB
/
day06.py
File metadata and controls
executable file
·38 lines (27 loc) · 1.09 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
#! /usr/bin/env python3
### vendor imports
import more_itertools
### local imports
import utils
def findUniqueCharactersEndpoint(datastream: str, length: int):
# Iterate through the datastream in a moving window of size `length`
for i, block in enumerate(more_itertools.windowed(datastream, length)):
# If the length of the unique character set is the same as the block,
# then all characters are unique
unique = set(block)
if len(block) == len(unique):
# Add the length, because we are returning the _endpoint_
return i + length
@utils.part1
def part1(puzzleInput: str):
datastream = puzzleInput.strip()
# Find unique characters in a 4 character window
utils.printAnswer(findUniqueCharactersEndpoint(datastream, 4))
# Return the datastream for part 2 to work with
return datastream
@utils.part2
def part2(_, datastream: str):
# The answer is found the same as before, only the window size is larger
utils.printAnswer(findUniqueCharactersEndpoint(datastream, 14))
if __name__ == "__main__":
utils.start()