OPEN 28/6



0by 0x tinyZED imageTEXT theTUBE

- conservation of creation -

zett

import hashlib

# Convenience function to generate initial state from a string of any length greater than 7 characters
def generate_initial_state(seed_string, length):
   seed_bytes = seed_string.encode('ascii')
   seed_hash = hashlib.sha256(seed_bytes).hexdigest()
   seed_bits = bin(int(seed_hash, 16))[2:].zfill(length)
   return [int(bit) for bit in seed_bits[:length]]

def rule_90(left, center, right):
   return left ^ right

def rule_150(left, center, right):
   return left ^ center ^ right

def update_ca(ca, rule_90_length):
   new_ca = [0] * len(ca)
   for i in range(1, len(ca) - 1):
       if i < rule_90_length:
           new_ca[i] = rule_90(ca[i-1], ca[i], ca[i+1])
       else:
           new_ca[i] = rule_150(ca[i-1], ca[i], ca[i+1])

   # Set the boundaries using zero
   new_ca[0] = rule_90(0, ca[0], ca[1])
   new_ca[-1] = rule_150(ca[-2], ca[-1], 0)
   return new_ca

def read_input_file(file_path):
   with open(file_path, 'rb') as file:
       input_bytes = file.read()
       input_bits = ''.join(f'{byte:08b}' for byte in input_bytes)
   return [int(bit) for bit in input_bits]

def write_output_file(file_path, output_bits):
   with open(file_path, 'wb') as file:
       for bit in output_bits:
           byte = bytes([0xFF]) if bit == 1 else bytes([0x00])
           file.write(byte)

def binary_to_integer(binary_list):
   return int("".join(map(str, binary_list)), 2)

def int_to_bits(number, number_of_bits):
   # Ensure the number is non-negative and within the range for the given number of bits
   if number < 0 or number >= 2 ** number_of_bits:
       raise ValueError("Number is out of range for the specified number of bits")
   
   # Generate the list of bits
   bits = []
   for i in range(number_of_bits - 1, -1, -1):
       bit = (number >> i) & 1
       bits.append(bit)
   
   return bits

def prepend_one(lst):
   return [1] + lst

def process_integer(n, m):
   # Find the closest multiple of ten which is less
   multiple_of_ten = ((n - m) // 10) * 10
   
   # Subtract and negate the result
   result = -(n - multiple_of_ten)
   
   # Initialize variables for adding powers of two
   power_of_two = 2
   count = 0
   
   # Add successive powers of two until result is positive or zero
   while result < 0:
       result += power_of_two
       power_of_two *= 2
       count += 1
   
   return result, count

# Generate the initial state from a string of any length greater than 7 characters
seed_string = "COmpREss"  # Example string with length greater than 7
ca_length = 65
ca = generate_initial_state(seed_string, ca_length)
rule_90_length = 64  # Apply Rule 90 to all but the last cell

# Read the input file as bits
input_bits = read_input_file('pycore04.py')

# Evolve the CA long enough to match the length of the input bits
steps_needed = len(input_bits)
ca_evolution = []
for step in range(steps_needed):
   ca_evolution.append(ca[0])
   ca = update_ca(ca, rule_90_length)

# Perform XOR operation with CA bits
output_bits = []
for i in range(len(input_bits)):
   output_bits.append(input_bits[i] ^ ca_evolution[i])
   
processing_count = 10
   
print(f'intermediate size: {len(output_bits)}')
   
# Perform the processing step 1000 times
for _ in range(processing_count):
   # Before prepend
   integer_sub = binary_to_integer(output_bits)
   
   # Prepend one to the bits
   output_bits = prepend_one(output_bits)
   
   # Convert binary list to integer
   integer_value = binary_to_integer(output_bits)
   
   # Process the integer to get 'result' and 'width'
   result, width = process_integer(integer_value, integer_sub)
   
   # Convert 'result' back to bits with the given 'width'
   output_bits = int_to_bits(result, width)

# Write the output bits to the output file
write_output_file('output_file.bin', output_bits)

print(f'file written: {len(output_bits)}')