Solved 2024/12 P2
This commit is contained in:
parent
9e72ebccf4
commit
f4bc6772f7
@ -123,8 +123,6 @@ So, it has a total price of `1930`.
|
|||||||
|
|
||||||
Your puzzle answer was `1437300`.
|
Your puzzle answer was `1437300`.
|
||||||
|
|
||||||
The first half of this puzzle is complete! It provides one gold star: \*
|
|
||||||
|
|
||||||
## \-\-- Part Two \-\-- {#part2}
|
## \-\-- Part Two \-\-- {#part2}
|
||||||
|
|
||||||
Fortunately, the Elves are trying to order so much fence that they
|
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?*
|
*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).
|
input](12/input).
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
#!/bin/python3
|
#!/bin/python3
|
||||||
import sys,time,re
|
import sys,time,re
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
import numpy as np
|
||||||
sys.path.insert(0, '../../')
|
sys.path.insert(0, '../../')
|
||||||
from fred import list2int,get_re,nprint,lprint,loadFile,toGrid,bfs,get_value_in_direction,addTuples,grid_valid
|
from fred import list2int,get_re,nprint,lprint,loadFile,toGrid,bfs,get_value_in_direction,addTuples,grid_valid
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
input_f = 'test'
|
input_f = 'input'
|
||||||
|
|
||||||
#########################################
|
#########################################
|
||||||
# #
|
# #
|
||||||
@ -78,7 +79,7 @@ def part1():
|
|||||||
|
|
||||||
|
|
||||||
start_time = time.time()
|
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)
|
visited.append(n)
|
||||||
neighbors += get_neighbors_and_corners(grid,n,visited)
|
neighbors += get_neighbors_and_corners(grid,n,visited)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return neighbors
|
return neighbors
|
||||||
|
|
||||||
|
def rotate_90_clockwise(grid:list) -> list:
|
||||||
|
return [list(row) for row in zip(*grid[::-1])]
|
||||||
|
|
||||||
def part2():
|
def part2():
|
||||||
grid = toGrid(input_f)
|
grid = toGrid(input_f)
|
||||||
|
#print(grid)
|
||||||
#nprint(grid)
|
#nprint(grid)
|
||||||
values = {}
|
values = {}
|
||||||
visited = []
|
visited = []
|
||||||
@ -136,7 +138,7 @@ def part2():
|
|||||||
|
|
||||||
for pdx,p in enumerate(total_plots):
|
for pdx,p in enumerate(total_plots):
|
||||||
total_plots[pdx] = sorted(p)
|
total_plots[pdx] = sorted(p)
|
||||||
pprint(total_plots)
|
#pprint(total_plots)
|
||||||
|
|
||||||
#directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
#directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||||
directions = [(0, 1), (1, 0)]
|
directions = [(0, 1), (1, 0)]
|
||||||
@ -164,12 +166,45 @@ def part2():
|
|||||||
print(end)
|
print(end)
|
||||||
|
|
||||||
for p in total_plots:
|
for p in total_plots:
|
||||||
|
area = len(p)
|
||||||
|
fences = 0
|
||||||
for r,c in p:
|
for r,c in p:
|
||||||
visited = []
|
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:
|
# for v in total_plots:
|
||||||
# total_corners = 0
|
# total_corners = 0
|
||||||
|
Binary file not shown.
2
fred.py
2
fred.py
@ -356,7 +356,7 @@ def ppprint(x):
|
|||||||
print(x[idx][jdx], end='')
|
print(x[idx][jdx], end='')
|
||||||
print()
|
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.
|
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.
|
If no direction is provided, returns the value at the current position.
|
||||||
|
Loading…
Reference in New Issue
Block a user