Add 2022 solutions
This commit is contained in:
41
2022/d6.py
Normal file
41
2022/d6.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import re
|
||||
from string import ascii_lowercase, ascii_uppercase
|
||||
|
||||
EXAMPLE = """
|
||||
mjqjpqmgbljsphdztnvjfqwrcgsmlb
|
||||
"""
|
||||
|
||||
def clean(text: str) -> list[str]:
|
||||
return list(filter(lambda l: l.strip() != "", text.splitlines()))
|
||||
|
||||
def solve(lines: list[str]):
|
||||
line = lines[0]
|
||||
for i in range(len(lines[0])):
|
||||
l = line[i:i + 4]
|
||||
if len(set(l)) == len(l):
|
||||
return i + 4
|
||||
# 2:55
|
||||
|
||||
def solve2(lines: list[str]):
|
||||
line = lines[0]
|
||||
for i in range(len(lines[0])):
|
||||
l = line[i:i + 14]
|
||||
if len(set(l)) == len(l):
|
||||
return i + 14
|
||||
# 3:50
|
||||
|
||||
def main():
|
||||
example = clean(EXAMPLE)
|
||||
print("Example 1:", solve(example))
|
||||
|
||||
data = clean(open("d6.txt").read())
|
||||
print("Solution 1:", solve(data))
|
||||
|
||||
example = clean(EXAMPLE)
|
||||
print("Example 2:", solve2(example))
|
||||
|
||||
data = clean(open("d6.txt").read())
|
||||
print("Solution 2:", solve2(data))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user