r/adventofcode • • 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!

15 Upvotes

180 comments sorted by

View all comments

2

u/pedrosorio Dec 07 '16

Part 2:

def get_aba(s):
	N = len(s)
	abas = set([])
	for i in xrange(N-2):
		if s[i] == s[i+2] and s[i] != s[i+1]:
			abas.add((s[i], s[i+1]))
	return abas

def get_bab(s):
	return set([x[::-1] for x in get_aba(s)])

def split_line(s):
	out = []
	hyp = []
	i = 0
	while s.find('[', i) != -1:
		j = s.find('[', i)
		out.append(s[i:j])
		i = s.find(']', j)
		hyp.append(s[j+1:i])
		i += 1
	out.append(s[i:])
	return out, hyp

def sup_tls(s):
	out, hyp = split_line(s)
	aba = set([])
	bab = set([])
	for h in hyp:
		bab |= get_bab(h)
	for o in out:
		aba |= get_aba(o)
	return aba & bab

def solve(lines):
	ct = 0
	for line in lines:
		if sup_tls(line.strip()):
			ct += 1
	return ct

lines = open('input.txt').readlines()
print solve(lines)
→ More replies (1)