Day 16 - Advent of Code 2015
Working solutions for the day 16 puzzles.
Part One
""" day_16_01.py """
# usage: python3 day_16_01.py < input
def makedict(text, separator):
""" convert key: value text to dict """
return dict([keyvalue.split(': ')
for keyvalue in text.strip().split(separator)])
def consistent(dict1, dict2):
""" are dict1 and dict2 logically consistent """
for key in dict1:
if key in dict2:
if dict1[key] != dict2[key]:
return False
return True
scan_results = """
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
"""
scan = makedict(scan_results, '\n')
while True:
try:
row = input()
except EOFError:
break
colon = row.find(':')
memory = makedict(row[colon + 2:], ', ')
if consistent(scan, memory):
print(row[4:colon])
Part Two
""" day_16_02.py """
# usage: python3 day_16_02.py < input
def makedict(text, separator):
""" convert key: value text to dict """
return dict([keyvalue.split(': ')
for keyvalue in text.strip().split(separator)])
def consistent(dict1, dict2):
""" are dict1 and dict2 logically consistent per puzzle """
for key in dict1:
if key in dict2:
if key in ['cats', 'trees']:
if int(dict1[key]) >= int(dict2[key]):
return False
elif key in ['pomeranians', 'goldfish']:
if int(dict1[key]) <= int(dict2[key]):
return False
else:
if dict1[key] != dict2[key]:
return False
return True
scan_results = """
children: 3
cats: 7
samoyeds: 2
pomeranians: 3
akitas: 0
vizslas: 0
goldfish: 5
trees: 3
cars: 2
perfumes: 1
"""
scan = makedict(scan_results, '\n')
while True:
try:
row = input()
except EOFError:
break
colon = row.find(':')
memory = makedict(row[colon + 2:], ', ')
if consistent(scan, memory):
print(row[4:colon])