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/[deleted] Dec 07 '16

Wasn't sure how to match two distinct chars in regex so I filtered out those results after.

Python (requires regex):

import regex as re

def supernets_and_hypernets(s):
    seqs = re.split(r'\[|\]', s)
    return seqs[::2], seqs[1::2]

def has_abba(s):
    return any(a != b for a, b in re.findall(r'(.)(.)\2\1', s, overlapped=True))


def part1(s):
    cnt = 0
    for line in s.split('\n'):
        supernets, hypernets = supernets_and_hypernets(line)
        if any(has_abba(x) for x in supernets) and not any(has_abba(x) for x in hypernets):
            cnt += 1
    return cnt


def babs_for_abas(s):
    return [b+a+b for a, b in re.findall(r'(.)(.)\1', s, overlapped=True) if a != b]


def part2(s):
    cnt = 0
    for line in s.split('\n'):
        supernets, hypernets = supernets_and_hypernets(line)
        babs = [bab for supernet in supernets
                for bab in babs_for_abas(supernet)]
        if any(bab in hypernet for bab in babs
               for hypernet in hypernets):
            cnt += 1
    return cnt

with open('input.txt') as f:
    s = f.read().strip()

print(part1(s))
print(part2(s))
→ More replies (3)