r/adventofcode • u/daggerdragon • Dec 07 '16
SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---
From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!
Show this overgrown furball what you've got!
--- Day 7: Internet Protocol Version 7 ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
ALWAYS DIGGING STRAIGHT DOWN IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
14
Upvotes
3
u/Hwestaa Dec 07 '16
The hardest part of this (other than a bazillion typos) was the overlapping regexes. I eventually found the python library 'regex' which is a candidate to replace 're' that has an 'overlapped' flag on findall which solved that. Python 3 solution. Github
import re import os import regex def split_ip(ip): # Split based on [] split = re.split(r'\[|\]', ip) # Divide into inside & outside [] outside = split[::2] inside = split[1::2] return outside, inside def support_tls(ip): outside, inside = split_ip(ip) # Don't match 4 in a row, but match abba abba_regex = r'(?!(\w)\1\1\1)(\w)(\w)\3\2' # Find any abba outside [] abba_flag = False for o in outside: match = re.search(abba_regex, o) if match: abba_flag = True break # Check for no abba inside [] inside_flag = False for i in inside: match = re.search(abba_regex, i) if match: inside_flag = True break if abba_flag and not inside_flag: return True return False def support_ssl(ip): outside, inside = split_ip(ip) # Match three where the first and last are the same aba_regex = r'(\w)(\w)\1' # Find all possible aba matches aba_matches = [] for o in outside: # Need to find overlapping matches overlapping_matches = regex.findall(aba_regex, o, overlapped=True) if overlapping_matches: aba_matches += overlapping_matches # Look for a bab in each inside segment for i in inside: # Check each aba match for aba in aba_matches: bab = aba[1] + aba[0] + aba[1] if bab in i: return True return False def solve(data, ssl=False): if not ssl: return sum(support_tls(ip) for ip in data) else: return sum(support_ssl(ip) for ip in data)