From 7dc684996277ca70b677bfea3b2bacafb70209b7 Mon Sep 17 00:00:00 2001 From: Dece Date: Sat, 14 Dec 2019 23:50:49 +0100 Subject: [PATCH] Day 14 --- 2019/day14.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2019/day14.py diff --git a/2019/day14.py b/2019/day14.py new file mode 100644 index 0000000..6ebbe1e --- /dev/null +++ b/2019/day14.py @@ -0,0 +1,53 @@ +import math +import re + + +def main(): + with open("day14.txt", "rt") as input_file: + lines = [line.rstrip() for line in input_file.readlines()] + line_re = re.compile(r"(.+) => (.+)") + raw_transmos = [line_re.match(line).groups() for line in lines] + transmos = {} + for inputs, outputs in raw_transmos: + mult, result = outputs.split(" ") + input_comps = [i.split(" ") for i in inputs.split(", ")] + input_tuples = [(int(q), n) for q, n in input_comps] + transmos[result] = int(mult), input_tuples + + print("Required ore for 1 FUEL:", get_required_ore(transmos, "FUEL", 1, {})) + +def get_required_ore(transmos, name, quantity, rabs): + if name == "ORE": + return quantity + + print("What is required for {} {}?".format(quantity, name)) + mult, comps = transmos[name] + num_reactions = math.ceil(quantity / mult) + + created = 0 + if name in rabs: + print(" Found {} in rab.".format(rabs[name])) + created += rabs[name] + rabs[name] = 0 + + req_ore = 0 + for _ in range(num_reactions): + if created >= quantity: + break + print(" Creation of {} {}...".format(mult, name)) + for rq, rn in comps: + req_ore += get_required_ore(transmos, rn, rq, rabs) + created += mult + print(" Possessing {} {}.".format(created, name)) + + if created > quantity: + leftover = created - quantity + rabs[name] = rabs.get(name, 0) + leftover + print(" Stored {} {} in rab.".format(leftover, name)) + + print("Answer ({} {}): {} ores".format(quantity, name, req_ore)) + return req_ore + + +if __name__ == "__main__": + main()