diff options
author | Prefetch | 2022-12-31 22:21:39 +0100 |
---|---|---|
committer | Prefetch | 2022-12-31 22:21:39 +0100 |
commit | 68615a9ad2c942254135cffb00cf25a84a3b1356 (patch) | |
tree | 1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /16/main.py |
Initial commit
Diffstat (limited to '16/main.py')
-rwxr-xr-x | 16/main.py | 66 |
1 files changed, 66 insertions, 0 deletions
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() |