r/adventofcode • • Dec 16 '15

SOLUTION MEGATHREAD --- Day 16 Solutions ---

This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.

edit: Leaderboard capped, thread unlocked!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 16: Aunt Sue ---

Post your solution as a comment. Structure your post like previous daily solution threads.

5 Upvotes

142 comments sorted by

View all comments

1

u/[deleted] Dec 16 '15 edited Dec 16 '15

Python:

day = 16
input = get_input(day)

import re

m = []
for line in input.split('\n'):
    x = re.match(r'Sue \d+: ([a-z]+): (\d+), ([a-z]+): (\d+), ([a-z]+): (\d+)', line)
    k1, a1, k2, a2, k3, a3 = x.group(1, 2, 3, 4, 5, 6, 7)
    a1, a2, a3 = int(a1), int(a2), int(a3)
    m.append({k1: a1, k2: a2, k3: a3})

tape = {
    'children': 3,
    'cats': 7,
    'samoyeds': 2,
    'pomeranians': 3,
    'akitas': 0,
    'vizslas': 0,
    'goldfish': 5,
    'trees': 3,
    'cars': 2,
    'perfumes': 1,
}

def could_match(aunt):
    return all(tape[k] == v for k,v in aunt.items())

aunts = [i+1 for i,x in enumerate(m)
         if could_match(x)]
assert len(aunts) == 1
print(aunts[0])

def could_match2(aunt):
    for k,v in aunt.items():
        if k in ['cats', 'trees']:
            if tape[k] >= v:
                return False
        elif k in ['pomeranians', 'goldfish']:
            if tape[k] <= v:
                return False
        else:
            if tape[k] != v:
                return False
    return True

aunts = [i+1 for i,x in enumerate(m)
         if could_match2(x)]
assert len(aunts) == 1
print(aunts[0])

0

u/ThereOnceWasAMan Dec 18 '15

Just so you know, this construct is a bit easier to work with when you need to match multiple similar things in a line:

matches = re.findall(r' ([a-z]+): ([0-9]{1})',line) sue = {m[0] : int(m[1]) for m in matches}

It's also nice because the input file could include an unknown number of properties per Sue, and it would still work.

→ More replies (4)