November 11, 2023

Solution for Advent of Code 2015 - Day 16: Aunt Sue

Link to the puzzle text

Part 1

In this puzzle we are given a few clues to find a person. There are multiple attributes and each person has either a concrete value or an unknown value. The clues on the other hand give a concrete value for each attribute. So the only way to rule out a person is if that person has a set attribute different from the clues.

After parsing the attributes via regex and setting unspecified attributes to "?", we iterate over all persons and see if the clues match. For this, we iterate over all attributes and see if the attribute for this person is either not set or set and same as the clue. If either is true, then we can not rule out this person. If after iterating over all attributes, we still could not rule out a person, we print out that persons id and hopefully there is only one such person.

The checking for matching attributes is done by the function invalid_attr below:

def invalid_attr(aunt, attr):
value = aunt[attr]
clue = clues[attr]
    return_value = value == "?" or value == clue
return return_value

for aunt in aunt_attributes:
valid = True
for attr in clues.keys():
if not valid:
continue
valid = valid and invalid_attr(aunt, attr)
if valid:
print(aunt["number"])

Part 2

For this part, we semantics of the clues changed. For two attributes, the clues now specify an upper value and for two other attributes a lower value. We extend our invalid_attr function above to handle these special cases by checking for < or > instead of equality. Again we iterate over all persons and attributes. We can rule out all but one person, who we print out as the solution.

def invalid_attr(aunt, attr):
value = aunt[attr]
clue = clues[attr]
if attr == "cats" or attr == "trees":
return_value = value == "?" or value > clue
elif attr == "pomeranians" or attr == "goldfish":
return_value = value == "?" or value < clue
else:
return_value = value == "?" or value == clue
return return_value

 

No comments:

Post a Comment