109 lines
2.8 KiB
Python
109 lines
2.8 KiB
Python
#!/bin/python3
|
|
import sys,time,re
|
|
from pprint import pprint
|
|
from collections import deque
|
|
sys.path.insert(0, '../../')
|
|
from fred import list2int,get_re,nprint,lprint,loadFile
|
|
start_time = time.time()
|
|
|
|
input_f = 'test'
|
|
|
|
part = 1
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
|
|
def rule1(number:int):
|
|
if number == 0:
|
|
return 1
|
|
else:
|
|
print('Number not 0, why are you here')
|
|
|
|
def rule2(number):
|
|
# Convert the number to a string
|
|
num_str = str(number)
|
|
length = len(num_str)
|
|
|
|
# Calculate the split index
|
|
middle = length // 2
|
|
|
|
# Split the number into two parts
|
|
left_part = num_str[:middle]
|
|
right_part = num_str[middle:]
|
|
#print(left_part,right_part)
|
|
|
|
return int(left_part), int(right_part)
|
|
|
|
def part1(input_f):
|
|
instructions = []
|
|
with open(input_f) as file:
|
|
instructions = list2int(file.readline().strip().split(' '))
|
|
new_inst = []
|
|
for x in range(25):
|
|
for idx,inst in enumerate(instructions):
|
|
|
|
# Rule 1
|
|
if inst == 0:
|
|
new_inst.append(1)
|
|
else:
|
|
# Rule 2
|
|
t = list(str(inst))
|
|
if len(t) % 2 == 0:
|
|
new_inst += rule2(inst)
|
|
else:
|
|
# Rule 3
|
|
new_inst.append(instructions[idx] * 2024)
|
|
instructions = new_inst
|
|
new_inst = []
|
|
|
|
print(len(instructions))
|
|
|
|
start_time = time.time()
|
|
part1(input_f)
|
|
print("--- %s seconds ---" % (time.time() - start_time))
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
|
|
def part2(input_f):
|
|
dq = []
|
|
with open(input_f) as file:
|
|
dq = deque(file.readline().strip().split(' '))
|
|
new_inst = deque([])
|
|
|
|
for r in range(75):
|
|
i = 0
|
|
while_time = time.time()
|
|
|
|
while i < len(dq):
|
|
if dq[i] == 0:
|
|
# Rule 1
|
|
new_inst.append(1)
|
|
else:
|
|
# Rule 2
|
|
if len(str(dq[i])) % 2 == 0:
|
|
|
|
x = rule2(dq[i])
|
|
new_inst.append(x[0])
|
|
new_inst.append(x[1])
|
|
|
|
else:
|
|
# Rule 3
|
|
new_inst.append(int(dq[i]) * 2024)
|
|
i += 1
|
|
|
|
dq = new_inst
|
|
new_inst = deque([])
|
|
print(r,len(dq),"--- %s seconds ---" % (time.time() - while_time))
|
|
|
|
#print()
|
|
print(len(dq))
|
|
|
|
start_time = time.time()
|
|
part2(input_f)
|
|
print("--- %s seconds ---" % (time.time() - start_time))
|