summaryrefslogtreecommitdiff
path: root/11
diff options
context:
space:
mode:
Diffstat (limited to '11')
-rwxr-xr-x11/main.py71
-rwxr-xr-x11/test.py23
2 files changed, 94 insertions, 0 deletions
diff --git a/11/main.py b/11/main.py
new file mode 100755
index 0000000..9939d42
--- /dev/null
+++ b/11/main.py
@@ -0,0 +1,71 @@
+#!/usr/bin/python
+
+
+
+def increment(string):
+ last = string[-1]
+ if last == "z":
+ return increment(string[0:-1]) + "a"
+ else:
+ return string[0:-1] + chr(ord(last) + 1)
+
+
+
+def solve_part1(old):
+ valid = False
+ while not valid:
+ new = increment(old)
+
+ # Rule 2: no "i", "o" or "l"
+ # Cheap to check and likely to fail, so we do this first
+ rule2 = True
+ for c in ["i", "o", "l"]:
+ if new.count(c) > 0:
+ rule2 = False
+ break
+ if not rule2:
+ old = new
+ continue
+
+ # Rule 1: one increasing straight of three letters
+ rule1 = False
+ ords = [ord(c) for c in new]
+ for i in range(len(new) - 2):
+ if ords[i + 1] == ords[i] + 1 and ords[i + 2] == ords[i] + 2:
+ rule1 = True
+ break
+
+ # Rule 3: two different pair of letters
+ rule3 = False
+ count = 0
+ taken = []
+ for i in range(len(new) - 1):
+ if new[i] not in taken and new[i] == new[i + 1]:
+ count += 1
+ taken.append(new[i])
+ if count >= 2:
+ rule3 = True
+
+ old = new
+ valid = rule1 and rule3
+
+ return new
+
+
+
+def solve_part2(old):
+ return solve_part1(solve_part1(old))
+
+
+
+def main():
+ # My personal input password
+ passwd = "vzbxkghb"
+
+ print("Part 1 solution:", solve_part1(passwd)) # "vzbxxyzz" for me
+ print("Part 2 solution:", solve_part2(passwd)) # "vzcaabcc" for me
+
+
+
+if __name__ == "__main__":
+ main()
diff --git a/11/test.py b/11/test.py
new file mode 100755
index 0000000..b77bd46
--- /dev/null
+++ b/11/test.py
@@ -0,0 +1,23 @@
+#!/usr/bin/python
+
+import unittest
+
+import main
+
+
+
+class ExamplesPart1(unittest.TestCase):
+ def test_example1(self):
+ old = "abcdefgh"
+ new = "abcdffaa"
+ self.assertEqual(main.solve_part1(old), new)
+
+ def test_example2(self):
+ old = "ghijklmn"
+ new = "ghjaabcc"
+ self.assertEqual(main.solve_part1(old), new)
+
+
+
+if __name__ == "__main__":
+ unittest.main()