| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- from __future__ import annotations
- import collections
- import sys
- import typing
- import cache
- def main() -> None:
- mat = sys.argv[1]
- recipes: list[Recipe] = cache.get('https://api.prunplanner.org/data/recipes')
- companies: dict[str, dict[str, CompanyOutput]] = cache.get(
- 'https://pmmg-products.github.io/reports/data/company-data-dec25.json')['individual']
- print(mat, '→')
- wrought = (recipe for recipe in recipes if mat in (i['Ticker'] for i in recipe['Inputs']) and len(recipe['Outputs']) == 1)
- output_mats = {}
- for recipe in sorted(wrought, key=lambda r: r['Outputs'][0]['Ticker']):
- (output,) = recipe['Outputs']
- (input,) = (i for i in recipe['Inputs'] if i['Ticker'] == mat)
- ratio = input['Amount'] / output['Amount']
- output_mats[output['Ticker']] = ratio
- print(f'\t{output["Ticker"]:3}:', ratio)
- companies_produced = companies_consumed = companies_consumed_80 = 0
- into = dict.fromkeys(output_mats.keys(), 0)
- for company in companies.values():
- if (co_production := company.get(mat)) is None:
- continue
- companies_produced += 1
- consumed = 0
- for output_mat, per_run_consumption in output_mats.items():
- if co_consumption := company.get(output_mat):
- total_consumption = co_consumption['amount'] * per_run_consumption
- consumed += total_consumption
- into[output_mat] += total_consumption
- if consumed > 0:
- companies_consumed += 1
- if consumed > 0.8 * co_production['amount']:
- companies_consumed_80 += 1
- print(f'{companies_produced} companies producing')
- print(f'{companies_consumed} companies consuming their own production')
- print(f'{companies_consumed_80} companies consuming 80%+ of their own production')
- recipes_per_output = collections.defaultdict(int)
- for recipe in recipes:
- for output in recipe['Outputs']:
- if output['Ticker'] in into:
- recipes_per_output[output['Ticker']] += 1
- for output_mat, total in sorted(into.items(), key=lambda kv: kv[1], reverse=True):
- alt = ''
- if (recipe_count := recipes_per_output[output_mat]) != 1:
- alt = f' ({recipe_count} recipes)'
- print(f'{output_mat:3}: {total:8,.0f}{alt}')
- class CompanyOutput(typing.TypedDict):
- amount: int
- class Recipe(typing.TypedDict):
- RecipeName: str
- BuildingTicker: str
- Inputs: list[RecipeMat]
- Outputs: list[RecipeMat]
- TimeMs: int
- class RecipeMat(typing.TypedDict):
- Ticker: str
- Amount: int
- if __name__ == '__main__':
- main()
|