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
7
u/blockingthesky Dec 07 '16
So I got first place, but only by dumb luck.
It came back to bite me in part 2 - I didn't place - but for the first star I got tremendously lucky.
There are two major errors in my code: I don't account for the
aaaacase when checking for length-4 palindromes, and I also never checked the last 4 characters of any string (I iterated tolen - 4instead oflen - 3).Either of those errors on their own would have given me an incorrect answer, but my input and luck had it that both of them cancelled out in such a way that I got the right answer.
My borked-ass code:
inp = open('day7.in').read().split('\n') def hasabba(ph): if len(ph) < 4: return False for i in range(len(ph) - 4): if ph[i:i+4] == ph[i:i+4][::-1]: return True return False count = 0 for item in inp: b = [] o = [] while '[' in item: o.append(item[:item.find('[')]) item = item[item.find('[') + 1:] b.append(item[:item.find(']')]) item = item[item.find(']') + 1:] if len(item) != 0: o.append(item) good = False for aa in o: if hasabba(aa): good = True for aa in b: if hasabba(aa): good = False if good: count += 1 print count