Finished 2017/21

This commit is contained in:
FrederikBaerentsen 2024-11-28 22:34:51 +01:00
parent 4b8bd62d63
commit 38567d32ab
2 changed files with 131 additions and 60 deletions

View File

@ -110,7 +110,20 @@ Thus, after `2` iterations, the grid contains `12` pixels that are *on*.
*How many pixels stay on* after `5` iterations?
To begin, [get your puzzle input](21/input).
Your puzzle answer was `162`.
Answer:
## \-\-- Part Two \-\-- {#part2}
*How many pixels stay on* after `18` iterations?
Your puzzle answer was `2264586`.
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2017) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](21/input).

View File

@ -6,12 +6,12 @@ from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int, toGrid, nprint,get_re
input_f = 'test'
input_f = 'input'
part = 1
#########################################
# #
# Part 1 #
# Part 1+2 #
# #
#########################################
@ -55,14 +55,18 @@ def rotate90(grid):
return np.rot90(rotatedGrid)
def splitGrid(grid):
#print('Splitting')
#nprint(grid)
size = len(grid)
block_size = 0
if size % 2 == 0:
block_size = 2
elif size % 3 == 0:
block_size = 3
else:
print('Grind is wrong at splitGrid()')
block_size = 3
#print('Size:',size,' Block_size:',block_size)
#else:
# print('Grind is wrong at splitGrid()')
blocks = []
@ -74,72 +78,126 @@ def splitGrid(grid):
block[bdx] = list(b)
#print(block)
#input()
blocks.append(block)
blocks.append(block)
return blocks
def get_all_transformations(grid):
grid = np.array(grid)
transformations = set()
for k in range(4):
rotated = np.rot90(grid, k)
# Add original rotation
if tuple(map(tuple, rotated)) not in transformations:
transformations.add(tuple(map(tuple, rotated)))
yield rotated
# Add horizontal flip of rotation
h_flip = np.flipud(rotated)
if tuple(map(tuple, h_flip)) not in transformations:
transformations.add(tuple(map(tuple, h_flip)))
yield h_flip
# Add vertical flip of rotation
v_flip = np.fliplr(rotated)
if tuple(map(tuple, v_flip)) not in transformations:
transformations.add(tuple(map(tuple, v_flip)))
yield v_flip
def findInst(grid,instructions):
found = False
new_grid = []
grid = grid
while not found:
try:
new_grid = instructions[grid2line(grid)]
found = True
except:
grid = rotate90(grid)
for transformed_grid in get_all_transformations(grid):
#print(grid2line(transformed_grid))
#input()
try:
new_grid = instructions[grid2line(transformed_grid)]
found = True
break
except:
continue
return new_grid
def transform_grid(input_grid):
# Determine the dimensions of the grid
num_subgrids = len(input_grid)
subgrid_rows = len(input_grid[0]) # Rows in each subgrid
subgrid_cols = len(input_grid[0][0]) # Columns in each subgrid
# Calculate the number of subgrids per row and column
subgrids_per_row = int(num_subgrids ** 0.5)
# Initialize the result
result = []
for half in [input_grid[:2], input_grid[2:]]:
for i in range(3): # Each subgrid has 3 rows
# Merge the rows from the two subgrids in the current half
row = ''.join([''.join(subgrid[i]) for subgrid in half])
# Process each row of subgrids
for subgrid_row_idx in range(subgrids_per_row):
# Merge corresponding rows from subgrids in the current "row of subgrids"
for row_idx in range(subgrid_rows):
row = ''.join(
''.join(input_grid[subgrid_row_idx * subgrids_per_row + col_idx][row_idx])
for col_idx in range(subgrids_per_row)
)
result.append(row)
return result
rr = 0
if part == 1:
instructions = toSet(input_f,r"^(.*) => (.*)$")
print(instructions)
blocks = []
mixed_grid = [[]]
for i in range(0,5):
size = len(grid)
nprint(grid)
print()
if size % 3 == 0 or size % 2 == 0:
if not need2split(grid):
new_grid = findInst(grid,instructions)
grid = line2grid(new_grid)
#print(grid)
else:
blocks = splitGrid(grid)
#print(blocks)
#print(len(blocks))
#print(blocks)
print(blocks)
input()
mixed_grid = []
for i in range(0,len(blocks)):
x = line2grid(findInst(blocks[i],instructions))
mixed_grid.append(x)
grid = transform_grid(mixed_grid)
#nprint(grid)
#input()
else:
print('Something is wrong with the grid size of ', size)
#input()
#########################################
# #
# Part 2 #
# #
#########################################
rr = 5
if part == 2:
exit()
rr = 18
instructions = toSet(input_f,r"^(.*) => (.*)$")
#print(instructions)
blocks = []
mixed_grid = [[]]
for i in range(0,rr):
size = len(grid)
print('i:',i)
#nprint(grid)
#print()
if size % 3 == 0 or size % 2 == 0:
if not need2split(grid):
new_grid = findInst(grid,instructions)
grid = line2grid(new_grid)
#print(grid)
else:
blocks = splitGrid(grid)
#print(blocks)
#pprint(blocks)
#print(len(blocks))
#print(blocks)
#print(blocks)
#input()
mixed_grid = []
for i in range(0,len(blocks)):
#print(findInst(blocks[i],instructions))
x = line2grid(findInst(blocks[i],instructions))
#print(x)
#input()
mixed_grid.append(x)
#print(mixed_grid)
grid = transform_grid(mixed_grid)
#nprint(grid)
#input()
else:
print('Something is wrong with the grid size of ', size)
#input()
count = 0
for i in grid:
count += i.count('#')
print(count)