Day 6 completed, part1.py is now executable

This commit is contained in:
Fedaya 2024-12-01 17:57:00 +01:00
parent ae98a486a9
commit d800679291
4 changed files with 44 additions and 0 deletions

3
day6/common.py Normal file
View File

@ -0,0 +1,3 @@
def parse(filename: str):
with open(filename) as f:
return f.readline()[:-1]

20
day6/part1.py Normal file
View File

@ -0,0 +1,20 @@
from common import parse
def all_different_characters(input: str) -> bool:
return all(map(lambda c: input.count(c) == 1, input))
def solve(input: str) -> int:
i = 0
while i < len(input) and not all_different_characters(input[i : i + 4]):
i += 1
return i + 4
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

20
day6/part2.py Normal file
View File

@ -0,0 +1,20 @@
from common import parse
def all_different_characters(input: str) -> bool:
return all(map(lambda c: input.count(c) == 1, input))
def solve(input: str) -> int:
i = 0
while i < len(input) and not all_different_characters(input[i : i + 14]):
i += 1
return i + 14
def main():
print(solve(parse("input")))
if __name__ == "__main__":
main()

1
template/part1.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python
from common import parse