From 7d9f1bac103c0e68a0ea87d0faeace022467fe2e Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Sun, 17 Mar 2024 15:23:14 -0700 Subject: [PATCH] machines computation --- calc2.py | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/calc2.py b/calc2.py index c92c84f..fcb9a9c 100644 --- a/calc2.py +++ b/calc2.py @@ -1,3 +1,5 @@ +import math +import typing as tp from functools import lru_cache import pdbp # noqa: F401 @@ -193,6 +195,13 @@ def add_recipe(targets: ResourceGroup, recipe: ResourceGroup, multiplier: float) } +def sum_recipies(recipies: tp.Iterable[ResourceGroup]) -> ResourceGroup: + result = {} + for recipe in recipies: + result = add_recipe(result, recipe, -1) + return result + + def reduce_to_base_resources(group: ResourceGroup, base: set[str]) -> tuple[ResourceGroup, ResourceGroup]: group = group.copy() @@ -219,8 +228,10 @@ def reduce_to_base_resources(group: ResourceGroup, base: set[str]) -> tuple[Reso return reduced, intermediates -def drop_machines(group: ResourceGroup) -> ResourceGroup: - return {k: v for k, v in group.items() if not k.startswith(tuple(MACHINES))} +def drop_machines(group: ResourceGroup) -> tuple[ResourceGroup, ResourceGroup]: + group_wo_machines = {k: v for k, v in group.items() if not k.startswith(tuple(MACHINES))} + group_machines = {k: v for k, v in group.items() if k.startswith(tuple(MACHINES))} + return group_wo_machines, group_machines def print_resource_group(group: ResourceGroup) -> None: @@ -266,7 +277,7 @@ for group in bus_groups: bus_inputs = add_recipe(bus_inputs, reduced, -1) -bus_inputs = drop_machines(bus_inputs) +bus_inputs, bus_machines = drop_machines(bus_inputs) print() print('main bus inputs') print_resource_group(bus_inputs) @@ -289,7 +300,7 @@ print_resource_group(intermediates) print('reduced') print_resource_group(chemical_inputs) -chemical_inputs = drop_machines(chemical_inputs) +chemical_inputs, chemical_machines = drop_machines(chemical_inputs) print() print('chemical processing inputs') print_resource_group(chemical_inputs) @@ -329,7 +340,7 @@ print() print('oil processing') print_resource_group(oil_inputs) -oil_inputs = drop_machines(oil_inputs) +oil_inputs, oil_machines = drop_machines(oil_inputs) if pg := oil_inputs.pop('petroleum-gas', 0) > 0: raise RuntimeError(f'petrolium-gas > 0: {pg=}') @@ -352,7 +363,7 @@ print('reduced') print_resource_group(smelting_inputs) # mining -mining_outputs = drop_machines(smelting_inputs) +mining_outputs, smelting_machines = drop_machines(smelting_inputs) mining_base = {'mining-drill', 'pumpjack', 'offshore-pump', 'steam'} mining_inputs, intermediates = reduce_to_base_resources(mining_outputs, mining_base) @@ -362,3 +373,27 @@ print('mining intermediates') print_resource_group(intermediates) print('reduced') print_resource_group(mining_inputs) +_, mining_machines = drop_machines(mining_inputs) + + +def despecify_machines(group: ResourceGroup) -> ResourceGroup: + # assembler[belt-y] -> assembler, etc. + result = {} + for resource, count in group.items(): + if '[' in resource: + resource = resource[: resource.find('[')] + count = math.ceil(count) + result[resource] = result.get(resource, 0) + count + return result + + +# TODO: assembers work at 0.75 efficiency... + +machines = sum_recipies([bus_machines, chemical_machines, oil_machines, smelting_machines, mining_machines]) +machines = despecify_machines(machines) +print() +print('machines (despecified)') +print_resource_group(machines) + +# TODO: total machine count +# TODO: belt bandwidth