From ed7aba80ad82f07a4d6900b11fac9236bd20e788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 6 Jan 2024 11:46:59 +0100 Subject: [PATCH] 2015 day 11. --- src/holt59/aoc/2015/day11.py | 50 +++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day11.txt | 1 + 2 files changed, 51 insertions(+) create mode 100644 src/holt59/aoc/2015/day11.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day11.txt diff --git a/src/holt59/aoc/2015/day11.py b/src/holt59/aoc/2015/day11.py new file mode 100644 index 0000000..16c553b --- /dev/null +++ b/src/holt59/aoc/2015/day11.py @@ -0,0 +1,50 @@ +import itertools +import sys + + +def is_valid(p: str) -> bool: + if any(c in "iol" for c in p): + return False + + if not any( + ord(a) + 1 == ord(b) and ord(b) + 1 == ord(c) + for a, b, c in zip(p, p[1:], p[2:]) + ): + return False + + if sum(len(list(g)) >= 2 for _, g in itertools.groupby(p)) < 2: + return False + + return True + + +assert not is_valid("hijklmmn") +assert not is_valid("abbceffg") +assert not is_valid("abbcegjk") +assert is_valid("abcdffaa") +assert is_valid("ghjaabcc") + + +def increment(p: str) -> str: + if p[-1] == "z": + return increment(p[:-1]) + "a" + elif p[-1] in "iol": + return p[:-1] + chr(ord(p[-1]) + 2) + else: + return p[:-1] + chr(ord(p[-1]) + 1) + + +def find_next_password(p: str) -> str: + while not is_valid(p): + p = increment(p) + return p + + +line = sys.stdin.read().strip() + + +answer_1 = find_next_password(line) +print(f"answer 1 is {answer_1}") + +answer_2 = find_next_password(increment(answer_1)) +print(f"answer 2 is {answer_2}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day11.txt b/src/holt59/aoc/inputs/holt59/2015/day11.txt new file mode 100644 index 0000000..5915be7 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day11.txt @@ -0,0 +1 @@ +cqjxjnds