Files
aocpy/2015/d16.py
2024-02-01 19:21:22 -05:00

49 lines
1.1 KiB
Python

import re
data = open(0).read().strip()
target = {
"children": 3,
"cats": 7,
"samoyeds": 2,
"pomeranians": 3,
"akitas": 0,
"vizslas": 0,
"goldfish": 5,
"trees": 3,
"cars": 2,
"perfumes": 1,
}
part_1 = False
for i, line in enumerate(data.splitlines()):
_, r = re.split(r"\d+: ", line)
d = {"index": i + 1}
kvs = r.split(", ")
for kv in kvs:
key, val = kv.split(": ")
val = int(val)
d[key] = val
if part_1:
for k, v in d.items():
if k in target and target[k] != v:
break
else:
print(d['index'])
else:
for k, v in d.items():
if k in target and (k == "cats" or k == "trees"):
if v > target[k]:
continue
else:
break
elif k in target and (k == "pomeranians" or k == "goldfish"):
if v < target[k]:
continue
else:
break
elif k in target and target[k] != v:
break
else:
print(d['index'])