From a7a6b86002b595bc167af72606b14c67ed1bdf8f Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 24 Nov 2021 22:25:42 +0000 Subject: init commit --- 11/input | 1 + 11/solution.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/solution_own.py | 18 ++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 11/input create mode 100644 11/solution.py create mode 100644 11/solution_own.py (limited to '11') diff --git a/11/input b/11/input new file mode 100644 index 0000000..5915be7 --- /dev/null +++ b/11/input @@ -0,0 +1 @@ +cqjxjnds diff --git a/11/solution.py b/11/solution.py new file mode 100644 index 0000000..5c044b3 --- /dev/null +++ b/11/solution.py @@ -0,0 +1,69 @@ +"""Advent of code 2015 day 11""" + +def next_password(password: str) -> str: + """Increment the password + "a" -> "b" + "az" -> "ba" + """ + if password[-1] == 'z': + return next_password(password[:-1]) + 'a' + else: + return password[:-1] + chr(ord(password[-1]) + 1) + +def has_straight(password: str) -> bool: + """Check if a password contains a sequence of at least three consecutive characters""" + for i in range(len(password) - 2): + if ord(password[i]) + 1 == ord(password[i+1]) and ord(password[i+1]) + 1 == ord(password[i+2]): + return True + return False + +def has_pair(password: str) -> bool: + """Check if a password contains at least one pair of letters""" + for i in range(len(password) - 1): + if password[i] == password[i+1]: + return True + return False + +def has_pairs(password: str) -> bool: + """Check if a password contains at least two different, non-overlapping pairs of letters""" + for i in range(len(password) - 1): + if password[i] == password[i+1]: + return has_pair(password[i+2:]) + return False + +def is_valid(password: str) -> bool: + """Check if password is valid + Password must: + - include one increasing straight of at least three letters + - may not contain i, o or l + - must contain at least two different, non-overlapping pairs of letters + "hijklmmn" -> False + "abbceffg" -> True + "abbcegjk" -> False + """ + if not has_straight(password): + return False + if not has_pairs(password): + return False + if 'i' in password or 'o' in password or 'l' in password: + return False + return True + +def part1(password: str) -> str: + """Find the next valid password""" + password = next_password(password) + while not is_valid(password): + password = next_password(password) + return password + +def part2(password: str) -> str: + """Find the next valid password""" + password = next_password(part1(password)) + while not is_valid(password): + password = next_password(password) + return password + +if __name__ == '__main__': + inp = 'cqjxjnds' + print(part1(inp)) + print(part2(inp)) diff --git a/11/solution_own.py b/11/solution_own.py new file mode 100644 index 0000000..0c45bc0 --- /dev/null +++ b/11/solution_own.py @@ -0,0 +1,18 @@ +def valid(p: str) -> bool: + if {'i', 'o', 'l'}.intersection(set(p)): + return False + +def next_password(p: str) -> str: + if p[-1] == 'z': + return next_password(p[:-1]) + 'a' + else: + return p[:-1] + chr(ord(p[-1]) + 1) + +pw = next_password('cqjxjnds') +while not valid(pw): + pw = next_password(pw) +print(pw) +pw = next_password(pw) +while not valid(pw): + pw = next_password(pw) +print(pw) -- cgit v1.2.3-54-g00ecf