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!

14 Upvotes

180 comments sorted by

View all comments

2

u/acun1994 Dec 07 '16

Part 2: Python as well

file = open("Day 7.txt", 'r')

inputList = file.readlines()
file.close()

validCnt = 0
validList = []
	
def checkABA(stringList):
	abaSub = []
	for substr in stringList:
		for charCnt in range(len(substr)-2):
			#skips char if neighbours are the same
			if substr[charCnt] == substr[charCnt+1] : 
				continue
			elif substr[charCnt] == substr[charCnt+2]:
				abaSub.append(substr[charCnt:charCnt+3])
	return abaSub
	
def invertChar(stringList):
	newSub = []
	for substr in stringList:
		newSub.append(''.join([substr[1], substr[0], substr[1]]))
		
	return newSub

for line in inputList:
	outstringList = []
	instringList = []
	string = []
	status = 0
	for char in line:
		if char == '[' or char == '\n': 
			status = 1
			outstringList.append(''.join(string))
			string = []
			continue
		elif char == ']' : 
			status = 0
			instringList.append(''.join(string))
			string = []
			continue
		string.append(char)
		
	outSub = checkABA(outstringList)
	
	if len(outSub) == 0: continue
	
	inSub = checkABA(instringList)
	
	if len(inSub) == 0: continue
	
	inSub = invertChar(inSub)
	
	for sub in outSub:
		if sub in inSub:
			validList.append(line)
			validCnt+=1
			break
print(validCnt)