From 8092c555a3b8ea8fb202aed3dbeb14904fbfedf7 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Wed, 11 Dec 2024 10:04:21 +0100 Subject: [PATCH] Solved 2024/11 P1 --- 2024/11/11.md | 98 ++++++++++++++++++++++++++++++++++++++++ 2024/11/solution.py | 108 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 2024/11/11.md create mode 100644 2024/11/solution.py diff --git a/2024/11/11.md b/2024/11/11.md new file mode 100644 index 0000000..bcfe10a --- /dev/null +++ b/2024/11/11.md @@ -0,0 +1,98 @@ +## \-\-- Day 11: Plutonian Pebbles \-\-- + +The ancient civilization on [Pluto](/2019/day/20) was known for its +ability to manipulate spacetime, and while The Historians explore their +infinite corridors, you\'ve noticed a strange set of physics-defying +stones. + +At first glance, they seem like normal stones: they\'re arranged in a +perfectly *straight line*, and each stone has a *number* engraved on it. + +The strange part is that every time you +blink, the stones +*change*. + +Sometimes, the number engraved on a stone changes. Other times, a stone +might *split in two*, causing all the other stones to shift over a bit +to make room in their perfectly straight line. + +As you observe them for a while, you find that the stones have a +consistent behavior. Every time you blink, the stones each +*simultaneously* change according to the *first applicable rule* in this +list: + +- If the stone is engraved with the number `0`, it is replaced by a + stone engraved with the number `1`. +- If the stone is engraved with a number that has an *even* number of + digits, it is replaced by *two stones*. The left half of the digits + are engraved on the new left stone, and the right half of the digits + are engraved on the new right stone. (The new numbers don\'t keep + extra leading zeroes: `1000` would become stones `10` and `0`.) +- If none of the other rules apply, the stone is replaced by a new + stone; the old stone\'s number *multiplied by 2024* is engraved on + the new stone. + +No matter how the stones change, their *order is preserved*, and they +stay on their perfectly straight line. + +How will the stones evolve if you keep blinking at them? You take a note +of the number engraved on each stone in the line (your puzzle input). + +If you have an arrangement of five stones engraved with the numbers +`0 1 10 99 999` and you blink once, the stones transform as follows: + +- The first stone, `0`, becomes a stone marked `1`. +- The second stone, `1`, is multiplied by 2024 to become `2024`. +- The third stone, `10`, is split into a stone marked `1` followed by + a stone marked `0`. +- The fourth stone, `99`, is split into two stones marked `9`. +- The fifth stone, `999`, is replaced by a stone marked `2021976`. + +So, after blinking once, your five stones would become an arrangement of +seven stones engraved with the numbers `1 2024 1 0 9 9 2021976`. + +Here is a longer example: + + Initial arrangement: + 125 17 + + After 1 blink: + 253000 1 7 + + After 2 blinks: + 253 0 2024 14168 + + After 3 blinks: + 512072 1 20 24 28676032 + + After 4 blinks: + 512 72 2024 2 0 2 4 2867 6032 + + After 5 blinks: + 1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32 + + After 6 blinks: + 2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2 + +In this example, after blinking six times, you would have `22` stones. +After blinking 25 times, you would have `55312` stones! + +Consider the arrangement of stones in front of you. *How many stones +will you have after blinking 25 times?* + +Your puzzle answer was `211306`. + +The first half of this puzzle is complete! It provides one gold star: \* + +## \-\-- Part Two \-\-- {#part2} + +The Historians sure are taking a long time. To be fair, the infinite +corridors *are* very large. + +*How many stones would you have after blinking a total of 75 times?* + +Answer: + +Although it hasn\'t changed, you can still [get your puzzle +input](11/input). + diff --git a/2024/11/solution.py b/2024/11/solution.py new file mode 100644 index 0000000..4e73f7d --- /dev/null +++ b/2024/11/solution.py @@ -0,0 +1,108 @@ +#!/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))