Advent of Code 2022 - 6

Day 6: Tuning Trouble

https://adventofcode.com/2022/day/6

跟昨天一樣,第一部分跟第二部份根本一樣的題目。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def detect(n):
with open('06.txt', 'r') as fp:
signal = fp.read().strip()
loc = [0] * 26
cnt = 0
for i, c in enumerate(signal):
i = i + 1
idx = ord(c) - ord('a')
if loc[idx] == 0:
cnt += 1
else:
if i - loc[idx] <= cnt:
cnt = min(i-loc[idx], cnt)
else:
cnt += 1
if cnt == n:
return i
loc[idx] = i
return None

Part One

1
print(detect(4))

Answer: 1544

Part Two

1
print(detect(14))

Answer: 2145