From 68615a9ad2c942254135cffb00cf25a84a3b1356 Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 31 Dec 2022 22:21:39 +0100 Subject: Initial commit --- 16/main.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 16/main.py (limited to '16/main.py') diff --git a/16/main.py b/16/main.py new file mode 100755 index 0000000..ce630a2 --- /dev/null +++ b/16/main.py @@ -0,0 +1,66 @@ +#!/usr/bin/python + + + +# Machine's readings given in puzzle text +readings = { + "children" : 3, + "cats" : 7, + "samoyeds" : 2, + "pomeranians" : 3, + "akitas" : 0, + "vizslas" : 0, + "goldfish" : 5, + "trees" : 3, + "cars" : 2, + "perfumes" : 1 +} + + + +def solve_partn(partn, lines): + # Parse input file into a list of dictionaries + aunts = [] + for line in lines: + words = line.split(" ") + + data = {} + for i in range(3): + key = words[2 + 2 * i].rstrip(":") + val = words[3 + 2 * i].rstrip(",") + data[key] = int(val) + + aunts.append(data) + + # Find the aunt who gave us the present + for i, a in enumerate(aunts): + is_target = True + for k in a.keys(): + if partn == 1: + is_target &= a[k] == readings[k] + else: # partn == 2 + if k == "cats" or k == "trees": + is_target &= a[k] > readings[k] + elif k == "pomeranians" or k == "goldfish": + is_target &= a[k] < readings[k] + else: + is_target &= a[k] == readings[k] + if is_target: + return i + 1 + + return -1 # Shouldn't happen + + + +def main(): + # Read properties of all Aunts Sue from input text file + with open("input.txt", "r") as f: + lines = f.read().splitlines() + + print("Part 1 solution:", solve_partn(1, lines)) # 213 for me + print("Part 2 solution:", solve_partn(2, lines)) # 323 for me + + + +if __name__ == "__main__": + main() -- cgit v1.2.3