diff --git a/2024/day10/input.text b/2024/day10/input.text new file mode 100644 index 0000000..4bf3a30 --- /dev/null +++ b/2024/day10/input.text @@ -0,0 +1 @@ +814 1183689 0 1 766231 4091 93836 46 \ No newline at end of file diff --git a/2024/day10/main.py b/2024/day10/main.py new file mode 100644 index 0000000..3b13e28 --- /dev/null +++ b/2024/day10/main.py @@ -0,0 +1,43 @@ +import functools + +with open('input.text', 'r') as file: + data = file.read() + +stones: dict[int, int] = {} +for n in data.split(): + n = int(n) + if n not in stones: + stones[n] = 1 + else: + stones[n] += 1 + +blinks = 75 + +@functools.cache +def process_stone(n: int) -> list[int]: + + if n == 0: + return [1] + elif (digits := len(str(n))) % 2 == 0: + split: int = digits // 2 + return [int(str(n)[:split]), int(str(n)[split:])] + else: + return [n * 2024] + +def blink(stones: dict[int, int]) -> dict[int, int]: + counts: dict[int, int] = {} + + for n, count in stones.items(): + + new_stones: list[int] = process_stone(n) + for new_stone in new_stones: + if new_stone not in counts: + counts[new_stone] = count + else: + counts[new_stone] += count + + return counts + +for i in range(blinks): + stones = blink(stones) + print(i + 1, sum(stones.values())) diff --git a/2024/day10/test.text b/2024/day10/test.text new file mode 100644 index 0000000..528f9d5 --- /dev/null +++ b/2024/day10/test.text @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/2024/day9/main.py b/2024/day9/main.py index de06b21..4501135 100644 --- a/2024/day9/main.py +++ b/2024/day9/main.py @@ -159,8 +159,7 @@ class DiskMap: self.moveFile(file_index, free_index) - - offset = len(self.blocks) - file_index +offset = len(self.blocks) - file_index diff --git a/2024/day9/part2.py b/2024/day9/part2.py new file mode 100644 index 0000000..bb388ba --- /dev/null +++ b/2024/day9/part2.py @@ -0,0 +1,77 @@ +with open('input.text', 'r') as file: + data = file.read() + +drive: list[int] = [] + +is_file = True +f_id = 0 +for c in data: + + if is_file: + drive.extend([f_id] * int(c)) + f_id += 1 + else: + drive.extend([-1] * int(c)) + + is_file = not is_file + +def print_drive(drive: list[int]) -> None: + + for i in drive: + if i != -1: + print(i, end='') + else: + print('.', end='') + + print() + +def compress_drive(drive: list[int]) -> list[int]: + max_f_id: int = max(drive) + while max_f_id != 0: + + file_index: int = drive.index(max_f_id) + size: int = drive.count(max_f_id) + + free_size = 0 + free_index = None + found_block = False + for i, id in enumerate(drive): + + if i >= file_index: + break + + if id == -1: + free_size += 1 + if not free_index: + free_index = i + else: + free_size = 0 + free_index = None + + if free_size >= size: + found_block = True + break + + if found_block: + drive[file_index:file_index + size] = [-1] * size + drive[free_index:free_index + size] = [max_f_id] * size + + max_f_id -= 1 + + return drive + +def checksum(drive: list[int]) -> int: + s = 0 + for i, id in enumerate(drive): + if id != -1: + s += i * id + + return s + +# print_drive(drive) +# print_drive(compress_drive(drive)) + +drive = compress_drive(drive) +print(checksum(drive)) + +