-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.py
40 lines (28 loc) · 801 Bytes
/
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
# 12/6/2022
# https://adventofcode.com/2022/day/6
import sys
from collections import deque
def readUntilCondition(text, condition, lookback=4):
buf = deque()
for i, c in enumerate(text):
buf.append(c)
while len(buf) > lookback:
buf.popleft()
if len(buf) == lookback and condition(buf):
return i + 1
return -1
def allCharactersUnique(buf):
s = set(buf)
return len(s) == len(buf)
def part1(text):
return readUntilCondition(text, allCharactersUnique, lookback=4)
def part2(text):
return readUntilCondition(text, allCharactersUnique, lookback=14)
def main():
fname = sys.argv[1]
with open(fname, 'r') as f:
text = f.read().strip()
print("Part 1: %s" % (part1(text),))
print("Part 2: %s" % (part2(text),))
if __name__ == '__main__':
main()