From 497e1c54f692d2ae942372d3f21b2aecb40e8084 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 21 Dec 2022 21:59:50 +0100 Subject: day 21 part 2 --- 22.21/part2.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 22.21/part2.py diff --git a/22.21/part2.py b/22.21/part2.py new file mode 100644 index 0000000..248a7fd --- /dev/null +++ b/22.21/part2.py @@ -0,0 +1,40 @@ +from sys import stdin + +monkeys = {} + +for line in stdin: + out = line[:4] + parts = line[6:].split() + if len(parts) == 1: parts = int(parts[0]) + monkeys[out] = parts + +def f(x): + monks = monkeys.copy() + monks["root"][1] = '-' + monks["humn"] = int(x) + def get(k): + m = monks[k] + if type(m) != int: + a = get(m[0]) + b = get(m[2]) + op = m[1] + if op == '+': m = a + b + if op == '-': m = a - b + if op == '*': m = a * b + if op == '/': m = a / b + monks[k] = m + return m + return get("root") + +x = 0 +# newton +while True: + a = f(x) + print(x, a) + if a == 0: break + + d = a - f(x-1) + if d == 0: + x += 1 + continue + x = x - int(a/d) -- cgit 1.4.1-2-gfad0