Solved 2024/12 P2

This commit is contained in:
FrederikBaerentsen 2024-12-12 20:02:54 +01:00
parent 9e72ebccf4
commit f4bc6772f7
4 changed files with 54 additions and 15 deletions

View File

@ -123,8 +123,6 @@ So, it has a total price of `1930`.
Your puzzle answer was `1437300`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2}
Fortunately, the Elves are trying to order so much fence that they
@ -203,8 +201,14 @@ Adding these together produces its new total price of `1206`.
*What is the new total price of fencing all regions on your map?*
Answer:
Your puzzle answer was `849332`.
Although it hasn\'t changed, you can still [get your puzzle
Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2024) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](12/input).

View File

@ -1,11 +1,12 @@
#!/bin/python3
import sys,time,re
from pprint import pprint
import numpy as np
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile,toGrid,bfs,get_value_in_direction,addTuples,grid_valid
start_time = time.time()
input_f = 'test'
input_f = 'input'
#########################################
# #
@ -78,7 +79,7 @@ def part1():
start_time = time.time()
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
print('Part 1:',part1(), '\t', round((time.time() - start_time)*1000), 'ms')
#########################################
@ -105,13 +106,14 @@ def get_neighbors_and_corners(grid,node,visited):
visited.append(n)
neighbors += get_neighbors_and_corners(grid,n,visited)
return neighbors
def rotate_90_clockwise(grid:list) -> list:
return [list(row) for row in zip(*grid[::-1])]
def part2():
grid = toGrid(input_f)
#print(grid)
#nprint(grid)
values = {}
visited = []
@ -136,7 +138,7 @@ def part2():
for pdx,p in enumerate(total_plots):
total_plots[pdx] = sorted(p)
pprint(total_plots)
#pprint(total_plots)
#directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
directions = [(0, 1), (1, 0)]
@ -164,12 +166,45 @@ def part2():
print(end)
for p in total_plots:
area = len(p)
fences = 0
for r,c in p:
visited = []
print('@',(r,c))
#print('@',(r,c),get_value_in_direction(grid,(r,c)))
find_edge(r,c,p,visited)
input()
if get_value_in_direction(grid,(r,c),'up') != grid[r][c] and get_value_in_direction(grid,(r,c),'left') != grid[r][c]:
#print('┌ corner - up+left')
fences+=1
if get_value_in_direction(grid,(r,c),'up') == grid[r][c] and get_value_in_direction(grid,(r,c),'left') == grid[r][c] and get_value_in_direction(grid,(r,c),'up-left') != grid[r][c]:
#print('└ corner - up+left+upleft')
fences+=1
if get_value_in_direction(grid,(r,c),'left') != grid[r][c] and get_value_in_direction(grid,(r,c),'down') != grid[r][c]:
#print('└ corner - left+down')
fences+=1
if get_value_in_direction(grid,(r,c),'left') == grid[r][c] and get_value_in_direction(grid,(r,c),'down') == grid[r][c] and get_value_in_direction(grid,(r,c),'down-left') != grid[r][c]:
#print('┐ corner - left+down+downleft')
fences+=1
if get_value_in_direction(grid,(r,c),'down') != grid[r][c] and get_value_in_direction(grid,(r,c),'right') != grid[r][c]:
#print('┘ corner - down+right')
fences+=1
if get_value_in_direction(grid,(r,c),'down') == grid[r][c] and get_value_in_direction(grid,(r,c),'right') == grid[r][c] and get_value_in_direction(grid,(r,c),'down-right') != grid[r][c]:
#print('┌ corner - down+right+downright')
fences+=1
if get_value_in_direction(grid,(r,c),'right') != grid[r][c] and get_value_in_direction(grid,(r,c),'up') != grid[r][c]:
#print('┐ corner - right+up')
fences+=1
if get_value_in_direction(grid,(r,c),'right') == grid[r][c] and get_value_in_direction(grid,(r,c),'up') == grid[r][c] and get_value_in_direction(grid,(r,c),'up-right') != grid[r][c]:
#print('└ corner - right+up+upright')
fences+=1
#┌ ┐
#└ ┘
#input()
result+=(area*fences)
# for v in total_plots:
# total_corners = 0

Binary file not shown.

View File

@ -356,7 +356,7 @@ def ppprint(x):
print(x[idx][jdx], end='')
print()
def get_value_in_direction(grid, position, direction=None, length=1, type: str = None):
def get_value_in_direction(grid:list, position:set, direction:str=None, length:int=1, type: str = None):
"""
Get the value(s) in a specified direction from a given position in a grid.
If no direction is provided, returns the value at the current position.