solved day10p2 and day11
This commit is contained in:
parent
7f3832bf97
commit
6adfe75f0e
1
2024/day10/input.text
Normal file
1
2024/day10/input.text
Normal file
@ -0,0 +1 @@
|
|||||||
|
814 1183689 0 1 766231 4091 93836 46
|
||||||
43
2024/day10/main.py
Normal file
43
2024/day10/main.py
Normal file
@ -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()))
|
||||||
1
2024/day10/test.text
Normal file
1
2024/day10/test.text
Normal file
@ -0,0 +1 @@
|
|||||||
|
125 17
|
||||||
@ -159,7 +159,6 @@ class DiskMap:
|
|||||||
|
|
||||||
|
|
||||||
self.moveFile(file_index, free_index)
|
self.moveFile(file_index, free_index)
|
||||||
|
|
||||||
offset = len(self.blocks) - file_index
|
offset = len(self.blocks) - file_index
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
77
2024/day9/part2.py
Normal file
77
2024/day9/part2.py
Normal file
@ -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))
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user