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? *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, '../../') sys.path.insert(0, '../../')
from fred import list2int, toGrid, nprint,get_re from fred import list2int, toGrid, nprint,get_re
input_f = 'test' input_f = 'input'
part = 1 part = 1
######################################### #########################################
# # # #
# Part 1 # # Part 1+2 #
# # # #
######################################### #########################################
@ -55,14 +55,18 @@ def rotate90(grid):
return np.rot90(rotatedGrid) return np.rot90(rotatedGrid)
def splitGrid(grid): def splitGrid(grid):
#print('Splitting')
#nprint(grid)
size = len(grid) size = len(grid)
block_size = 0 block_size = 0
if size % 2 == 0: if size % 2 == 0:
block_size = 2 block_size = 2
elif size % 3 == 0:
block_size = 3
else: 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 = [] blocks = []
@ -77,39 +81,89 @@ def splitGrid(grid):
blocks.append(block) blocks.append(block)
return blocks 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): def findInst(grid,instructions):
found = False found = False
new_grid = [] new_grid = []
grid = grid grid = grid
while not found: while not found:
for transformed_grid in get_all_transformations(grid):
#print(grid2line(transformed_grid))
#input()
try: try:
new_grid = instructions[grid2line(grid)] new_grid = instructions[grid2line(transformed_grid)]
found = True found = True
break
except: except:
grid = rotate90(grid) continue
return new_grid return new_grid
def transform_grid(input_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 = [] result = []
for half in [input_grid[:2], input_grid[2:]]: # Process each row of subgrids
for i in range(3): # Each subgrid has 3 rows for subgrid_row_idx in range(subgrids_per_row):
# Merge the rows from the two subgrids in the current half # Merge corresponding rows from subgrids in the current "row of subgrids"
row = ''.join([''.join(subgrid[i]) for subgrid in half]) 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) result.append(row)
return result return result
rr = 0
if part == 1: if part == 1:
instructions = toSet(input_f,r"^(.*) => (.*)$") rr = 5
if part == 2:
rr = 18
instructions = toSet(input_f,r"^(.*) => (.*)$")
print(instructions) #print(instructions)
blocks = [] blocks = []
mixed_grid = [[]] mixed_grid = [[]]
for i in range(0,5): for i in range(0,rr):
size = len(grid) size = len(grid)
nprint(grid) print('i:',i)
print() #nprint(grid)
#print()
if size % 3 == 0 or size % 2 == 0: if size % 3 == 0 or size % 2 == 0:
if not need2split(grid): if not need2split(grid):
new_grid = findInst(grid,instructions) new_grid = findInst(grid,instructions)
@ -118,17 +172,24 @@ if part == 1:
else: else:
blocks = splitGrid(grid) blocks = splitGrid(grid)
#print(blocks) #print(blocks)
#pprint(blocks)
#print(len(blocks)) #print(len(blocks))
#print(blocks) #print(blocks)
print(blocks) #print(blocks)
input() #input()
mixed_grid = [] mixed_grid = []
for i in range(0,len(blocks)): for i in range(0,len(blocks)):
#print(findInst(blocks[i],instructions))
x = line2grid(findInst(blocks[i],instructions)) x = line2grid(findInst(blocks[i],instructions))
#print(x)
#input()
mixed_grid.append(x) mixed_grid.append(x)
#print(mixed_grid)
grid = transform_grid(mixed_grid) grid = transform_grid(mixed_grid)
#nprint(grid) #nprint(grid)
#input() #input()
@ -136,10 +197,7 @@ if part == 1:
else: else:
print('Something is wrong with the grid size of ', size) print('Something is wrong with the grid size of ', size)
#input() #input()
######################################### count = 0
# # for i in grid:
# Part 2 # count += i.count('#')
# # print(count)
#########################################
if part == 2:
exit()