Advent of Code 2016 solutions https://adventofcode.com/2016/
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.

07.py 1.1KB

1234567891011121314151617181920212223242526
  1. #!/usr/bin/python3
  2. import re
  3. # Returns True if x contains an ABBA sequence
  4. abba = lambda x: re.match(r'.*(.)(?!\1)(.)\2\1.*', x)
  5. # Returns all trigrams in x
  6. tris = lambda x: [x[i:i+3] for i in range(len(x) - 2) if '#' not in x[i:i+3]]
  7. # Returns all ABA sequences in x
  8. abas = lambda x: [t for t in tris(x) if t[0] == t[2] and t[1] != t[0]]
  9. # Swaps an ABA trigram into BAB
  10. swap = lambda x: x[1] + x[0] + x[1]
  11. # Returns all hypernet parts (between brackets) of x
  12. hypernet = lambda x: re.sub(r'^.*?\[|\].*?\[|\].*?$', '#', x)
  13. # Returns all supernet parts (outside of brackets) of x
  14. supernet = lambda x: re.sub(r'\[.*?\]', '#', x)
  15. with open('data/07.txt', 'r') as file:
  16. ips = list(map(str.strip, file.readlines()))
  17. # tls: has at least one ABBA that's not also in its hypernet sections
  18. tls = set(filter(abba, ips)) - set(filter(lambda ip: abba(hypernet(ip)), ips))
  19. # ssl: has an ABA in the supernet and a BAB in the hypernet
  20. ssl = [ip for ip in ips if set(map(swap, abas(supernet(ip)))).intersection(set(abas(hypernet(ip))))]
  21. print("Part one: %s" % len(tls))
  22. print("Part two: %s" % len(ssl))