From 7c1ddf5b277eab6009d91a8a738ab2de2403299d Mon Sep 17 00:00:00 2001 From: Dmitry Fedotov Date: Tue, 31 Dec 2024 18:36:06 +0300 Subject: [PATCH] add day 11 --- day11/code.py | 44 ++++++++++++++++++++++++++++++ day11/input.txt | 1 + day11/input_test.txt | 1 + day11/task.txt | 65 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 day11/code.py create mode 100644 day11/input.txt create mode 100644 day11/input_test.txt create mode 100644 day11/task.txt diff --git a/day11/code.py b/day11/code.py new file mode 100644 index 0000000..1bc154a --- /dev/null +++ b/day11/code.py @@ -0,0 +1,44 @@ +import sys +sys.path.append('../aoclib') + +from aoclib import Input + +def apply_rules(stones: dict[int, int]) -> dict[int, int]: + new_stones = dict() + for k in stones.keys(): + new_value = _apply_rules(k) + for item in new_value: + if item not in new_stones: + new_stones[item] = 0 + new_stones[item] += stones[k] + return new_stones + +def _apply_rules(s: int) -> list[int]: + res = list() + if s == 0: + res.append(1) + elif len(str(s))%2==0: + _s = str(s) + res.extend([int(_s[:len(_s)//2]), int(_s[len(_s)//2:])]) + else: + res.append(s*2024) + return res + + +def solve(iter: int, stones: list[int]) -> int: + s = dict(zip(stones, [1 for _ in range(len(stones))])) + for st in stones: + s[st] = 1 + for i in range(iter): + s = apply_rules(s) + return sum(s.values()) + +if __name__ == '__main__': + #stones = Input('input_test.txt').lines_as_int_lists() + stones = Input('input.txt').lines_as_int_lists() + + #part 1 + print('part 1:', solve(25, stones[0])) # 216996 + + #part 2 + print('part 2:', solve(75, stones[0])) # 257335372288947 diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..75a6804 --- /dev/null +++ b/day11/input.txt @@ -0,0 +1 @@ +0 7 198844 5687836 58 2478 25475 894 diff --git a/day11/input_test.txt b/day11/input_test.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/day11/input_test.txt @@ -0,0 +1 @@ +125 17 diff --git a/day11/task.txt b/day11/task.txt new file mode 100644 index 0000000..e13e2e4 --- /dev/null +++ b/day11/task.txt @@ -0,0 +1,65 @@ +--- Day 11: Plutonian Pebbles --- +The ancient civilization on Pluto 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 216996. + +The first half of this puzzle is complete! It provides one gold star: * + +--- Part Two --- +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? + +Your puzzle answer was 257335372288947. + +Both parts of this puzzle are complete! They provide two gold stars: **