Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2015-12-16 - Aunt Sue

(original .ipynb)

Puzzle input is a set of properties that we know about ~500 aunties called Sue. Part one involves taking a set of readings and matching them against a given sue. Part two involves taking these readings and matching them in a slightly different way to find the correct Sue.

from collections import defaultdict

puzzle_input_str = open("./puzzle_input/day16.txt").read()


def parse_sue(line):
    details = dict()
    tokens = line.split(" ")
    i = 2
    while i < len(tokens):
        details[tokens[i][:-1]] = int(tokens[i+1].rstrip(","))
        i += 2
    return int(tokens[1][:-1]), details


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


def approximate_match(sue):
    for prop in sue:
        if sue[prop] != readings[prop]:
            return False
    return True


def part_one(input_str, match_func=approximate_match):
    for line in input_str.split("\n"):
        i, sue = parse_sue(line)
        if match_func(sue):
            return i


print("part one:", part_one(puzzle_input_str))
part one: 103
default_match          = lambda target, actual: target != actual
decayed_match          = lambda target, actual: target >= actual
magnetoreluctant_match = lambda target, actual: target <= actual
matches = {
    "cats":        decayed_match,
    "trees":       decayed_match,
    "pomeranians": magnetoreluctant_match,
    "goldfish":    magnetoreluctant_match,
    "children":    default_match,
    "samoyeds":    default_match,
    "akitas":      default_match,
    "vizslas":     default_match,
    "cars":        default_match,
    "perfumes":    default_match
}


def adjusted_match(sue):
    for prop in sue:
        comp = matches[prop]
        if comp(readings[prop], sue[prop]):
            return False
    return True


def part_two(input_str):
    return part_one(input_str, adjusted_match)


print("part two:", part_two(puzzle_input_str))
part two: 405