day 11 ⏳⌛🌌
This commit is contained in:
parent
c13bab03bd
commit
7a15674dbf
219
2024/Day 11/gold.py
Normal file
219
2024/Day 11/gold.py
Normal file
@ -0,0 +1,219 @@
|
|||||||
|
import time
|
||||||
|
import cProfile
|
||||||
|
import line_profiler
|
||||||
|
#from rich import print
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
with open('puzzle.txt', 'r') as f:
|
||||||
|
line = f.read()
|
||||||
|
|
||||||
|
stones = {}
|
||||||
|
|
||||||
|
for stone in list(map(int, line.split(' '))):
|
||||||
|
length = len(str(stone))
|
||||||
|
splittable = True if length % 2 == 0 else False
|
||||||
|
|
||||||
|
if splittable:
|
||||||
|
half = 10 ** (length // 2)
|
||||||
|
first_half = stone // half
|
||||||
|
second_half = stone % half
|
||||||
|
|
||||||
|
stones[stone] = {
|
||||||
|
'number': 1,
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable,
|
||||||
|
'first_half': first_half,
|
||||||
|
'second_half': second_half
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones[stone] = {
|
||||||
|
'number': 1,
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable
|
||||||
|
}
|
||||||
|
|
||||||
|
@line_profiler.profile
|
||||||
|
def run_puzzle(stones):
|
||||||
|
for i in range(0, 75):
|
||||||
|
loop_start = time.time()
|
||||||
|
stones_update = {}
|
||||||
|
for stone in stones:
|
||||||
|
if stones[stone]['number'] > 0:
|
||||||
|
if stones[stone]['splittable']:
|
||||||
|
if stones[stone]['first_half'] in stones_update:
|
||||||
|
stones_update[stones[stone]['first_half']]['number'] += stones[stone]['number']
|
||||||
|
|
||||||
|
else:
|
||||||
|
if stones[stone]['first_half'] in stones:
|
||||||
|
if stones[stones[stone]['first_half']]['splittable']:
|
||||||
|
stones_update[stones[stone]['first_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stones[stone]['first_half']]['length'],
|
||||||
|
'splittable': stones[stones[stone]['first_half']]['splittable'],
|
||||||
|
'first_half': stones[stones[stone]['first_half']]['first_half'],
|
||||||
|
'second_half': stones[stones[stone]['first_half']]['second_half']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[stones[stone]['first_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stones[stone]['first_half']]['length'],
|
||||||
|
'splittable': stones[stones[stone]['first_half']]['splittable']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
length = len(str(stones[stone]['first_half']))
|
||||||
|
splittable = True if length % 2 == 0 else False
|
||||||
|
|
||||||
|
if splittable:
|
||||||
|
half = 10 ** (length // 2)
|
||||||
|
first_half = stones[stone]['first_half'] // half
|
||||||
|
second_half = stones[stone]['first_half'] % half
|
||||||
|
|
||||||
|
stones_update[stones[stone]['first_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable,
|
||||||
|
'first_half': first_half,
|
||||||
|
'second_half': second_half
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[stones[stone]['first_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable
|
||||||
|
}
|
||||||
|
|
||||||
|
if stones[stone]['second_half'] in stones_update:
|
||||||
|
stones_update[stones[stone]['second_half']]['number'] += stones[stone]['number']
|
||||||
|
|
||||||
|
else:
|
||||||
|
if stones[stone]['second_half'] in stones:
|
||||||
|
if stones[stones[stone]['second_half']]['splittable']:
|
||||||
|
stones_update[stones[stone]['second_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stones[stone]['second_half']]['length'],
|
||||||
|
'splittable': stones[stones[stone]['second_half']]['splittable'],
|
||||||
|
'first_half': stones[stones[stone]['second_half']]['first_half'],
|
||||||
|
'second_half': stones[stones[stone]['second_half']]['second_half']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[stones[stone]['second_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stones[stone]['second_half']]['length'],
|
||||||
|
'splittable': stones[stones[stone]['second_half']]['splittable']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
length = len(str(stones[stone]['second_half']))
|
||||||
|
splittable = True if length % 2 == 0 else False
|
||||||
|
|
||||||
|
if splittable:
|
||||||
|
half = 10 ** (length // 2)
|
||||||
|
first_half = stones[stone]['second_half'] // half
|
||||||
|
second_half = stones[stone]['second_half'] % half
|
||||||
|
|
||||||
|
stones_update[stones[stone]['second_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable,
|
||||||
|
'first_half': first_half,
|
||||||
|
'second_half': second_half
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[stones[stone]['second_half']] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable
|
||||||
|
}
|
||||||
|
|
||||||
|
elif stone != 0:
|
||||||
|
number = stone * 2024
|
||||||
|
if number in stones_update:
|
||||||
|
stones_update[number]['number'] += stones[stone]['number']
|
||||||
|
|
||||||
|
else:
|
||||||
|
if number in stones:
|
||||||
|
if stones[number]['splittable']:
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stone]['length'],
|
||||||
|
'splittable': stones[number]['splittable'],
|
||||||
|
'first_half': stones[number]['first_half'],
|
||||||
|
'second_half': stones[number]['second_half']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[number]['length'],
|
||||||
|
'splittable': stones[number]['splittable']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
length = len(str(number))
|
||||||
|
splittable = True if length % 2 == 0 else False
|
||||||
|
|
||||||
|
if splittable:
|
||||||
|
half = 10 ** (length // 2)
|
||||||
|
first_half = number // half
|
||||||
|
second_half = number % half
|
||||||
|
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable,
|
||||||
|
'first_half': first_half,
|
||||||
|
'second_half': second_half
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': length,
|
||||||
|
'splittable': splittable
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
number = 1
|
||||||
|
if number in stones_update:
|
||||||
|
stones_update[number]['number'] += stones[stone]['number']
|
||||||
|
|
||||||
|
else:
|
||||||
|
if number in stones:
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': stones[stone]['length'],
|
||||||
|
'splittable': stones[stone]['splittable']
|
||||||
|
}
|
||||||
|
|
||||||
|
else:
|
||||||
|
stones_update[number] = {
|
||||||
|
'number': stones[stone]['number'],
|
||||||
|
'length': 1,
|
||||||
|
'splittable': False
|
||||||
|
}
|
||||||
|
|
||||||
|
stones[stone]['number'] = 0
|
||||||
|
|
||||||
|
stones.update(stones_update)
|
||||||
|
|
||||||
|
print(f'loop {i} completed in {time.time() - loop_start} seconds')
|
||||||
|
|
||||||
|
return stones
|
||||||
|
|
||||||
|
|
||||||
|
cProfile.run('run_puzzle(stones)')
|
||||||
|
|
||||||
|
stone_total = 0
|
||||||
|
|
||||||
|
for stone in stones:
|
||||||
|
stone_total += stones[stone]['number']
|
||||||
|
|
||||||
|
print(stone_total)
|
||||||
|
print(f'Total time: {time.time() - start_time}')
|
33
2024/Day 11/main.py
Normal file
33
2024/Day 11/main.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import time
|
||||||
|
import copy
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
with open('puzzle.txt', 'r') as f:
|
||||||
|
line = f.read()
|
||||||
|
|
||||||
|
stones = line.split(' ')
|
||||||
|
|
||||||
|
for i in range(0, 25):
|
||||||
|
loop_start = time.time()
|
||||||
|
len_increased = 0
|
||||||
|
for index, stone in enumerate(copy.deepcopy(stones)):
|
||||||
|
match stone:
|
||||||
|
case '0':
|
||||||
|
stones[index + len_increased] = '1'
|
||||||
|
|
||||||
|
case stone if len(stone) % 2 == 0:
|
||||||
|
divide_index = len(stone) // 2
|
||||||
|
first_half = str(int(stone[:divide_index]))
|
||||||
|
second_half = str(int(stone[divide_index:]))
|
||||||
|
stones[index + len_increased] = first_half
|
||||||
|
stones.insert(index + len_increased + 1, second_half)
|
||||||
|
len_increased += 1
|
||||||
|
|
||||||
|
case _:
|
||||||
|
stones[index + len_increased] = str(int(stone) * 2024)
|
||||||
|
|
||||||
|
print(f'loop {i} completed in {time.time() - start_time} seconds')
|
||||||
|
|
||||||
|
print(len(stones))
|
||||||
|
print(f'Total time: {time.time() - start_time}')
|
1
2024/Day 11/puzzle.txt
Normal file
1
2024/Day 11/puzzle.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
6563348 67 395 0 6 4425 89567 739318
|
Loading…
x
Reference in New Issue
Block a user