Advent of Code: Day 7 Solution

Regex madness

Part one was easy. Part two was much harder today. Fo the most part both parts today were solved by regexes. My solution utilized regex within python to solve the puzzle.

As always my solution is also available on Github.

Once again, both parts were solved with the same script without much modification necessary. This is how I hope most days end up being.

Day 07:

import re

# Read in the input file.
with open ("input.txt", "r") as myfile:  

def get_abas(word):  
    abas = []
    for i in range(0, len(word) - 2):
        slc = word[i:i+3]
        if slc[0] == slc[2] and slc[0] != slc[1]:
    return abas

def isTLS(ipaddress):  
    match ='\[[^\]]*(\w)(\w)\2\1.*\]', ipaddress)
    if ((match is not None) and not ( ==
        return False

    match ='(\w)(\w)\2\1', ipaddress)
    return ((match is not None) and not ( ==

def isSSL(ipaddress):  
    regex = re.compile('.*\[([a-z]+)\].*')
    hypernets = []
    while True:
        m = regex.match(ipaddress)
        if not m:
        inner = m.groups()[0]
        ipaddress = ipaddress.replace(inner, '')
    abas = get_abas(ipaddress)
    for a in abas:
        bab = ''.join( [ a[1], a[0], a[1] ] )
        for h in hypernets:
            if bab in h:
                return True
    return False

tls_count = 0  
ssl_count = 0  
for line in data:  
    if isTLS(line):
        tls_count += 1
    if isSSL(line):
        ssl_count += 1

print "tls: " + str(tls_count)  
print "ssl: " + str(ssl_count)  

for reference, my input file is here, and my results were:

  • Part 1: 110
  • Part 2: 242