Advent of code 2015 solutions https://adventofcode.com/2015/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11.py 958B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python
  2. from string import ascii_lowercase
  3. from itertools import takewhile
  4. import re
  5. input = 'cqjxjnds'
  6. run_regex = '|'.join(ascii_lowercase[i:i+3] for i in range(24))
  7. next_letter = dict((l, chr(ord(l) + 1)) for l in ascii_lowercase)
  8. next_letter['h'] = 'j'
  9. next_letter['n'] = 'p'
  10. next_letter['k'] = 'm'
  11. next_letter['z'] = 'a'
  12. def increment(str):
  13. tail = sum(1 for _ in takewhile(lambda x: x == 'z', str[::-1]))
  14. next = list(str)
  15. if tail < len(next):
  16. next[-tail - 1] = next_letter[next[-tail - 1]]
  17. if tail > 0:
  18. next[-tail:] = 'a' * tail
  19. return ''.join(next)
  20. def matches(str):
  21. has_run = re.search(run_regex, str)
  22. has_bad_letters = re.search('[iol]', str)
  23. has_pairs = re.search(r'(.)(\1).*?(?!\1)(.)(\3)', str)
  24. return has_run and has_pairs and not has_bad_letters
  25. for _ in range(2):
  26. input = increment(input)
  27. while not matches(input):
  28. input = increment(input)
  29. print(input)