filename = "pokemon.txt"
total_cap = 600 # new total cap
factor_cap = 1.5 # new factor cap
max_value = 255
def calculate_new_values(nums, total_cap, factor_cap):
original_total = sum(nums)
if original_total < total_cap:
factor = min(total_cap / original_total, factor_cap)
new_first_num = nums[0] # Store the first number before modifying
new_rest_nums = [int(min(num * factor, max_value)) for num in nums[1:]]
new_total = new_first_num + sum(new_rest_nums)
while new_total > total_cap:
# Adjust the first number iteratively to ensure the total is not exceeded
new_first_num = int(min(new_first_num - 1, max_value))
new_rest_nums = [int(min(num * factor, max_value)) for num in nums[1:]]
new_total = new_first_num + sum(new_rest_nums)
new_nums = [new_first_num] + new_rest_nums
else:
new_nums = nums
return new_nums
with open(filename, "r") as f:
lines = f.readlines()
for i in range(len(lines)):
line = lines[i]
if line.startswith("BaseStats = "):
nums = line.split("=")[1].strip().split(",")
try:
nums = [int(num) for num in nums] # Convert back to integers
new_nums = calculate_new_values(nums, total_cap, factor_cap)
new_line = "BaseStats = " + ",".join(map(str, new_nums)) + "\n"
lines[i] = new_line
except ValueError:
# Handle invalid input format gracefully
pass
with open("output.txt", "w") as f:
f.writelines(lines)