Working on 2024/12 P2
This commit is contained in:
parent
ac8bce83b9
commit
9e72ebccf4
@ -2,7 +2,7 @@
|
||||
import sys,time,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int,get_re,nprint,lprint,loadFile,toGrid,bfs,get_value_in_direction,addTuples
|
||||
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'
|
||||
@ -86,6 +86,29 @@ print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
def get_neighbors_and_corners(grid,node,visited):
|
||||
|
||||
directions = ['up','down','left','right']
|
||||
offsets = {
|
||||
'up': (-1, 0),
|
||||
'down': (1, 0),
|
||||
'left': (0, -1),
|
||||
'right': (0, 1),
|
||||
}
|
||||
neighbors = []
|
||||
for d in directions:
|
||||
if get_value_in_direction(grid,node,d) == get_value_in_direction(grid,node):
|
||||
n = addTuples(offsets[d],node)
|
||||
if n not in visited:
|
||||
neighbors.append(n)
|
||||
visited.append(n)
|
||||
neighbors += get_neighbors_and_corners(grid,n,visited)
|
||||
|
||||
|
||||
|
||||
return neighbors
|
||||
|
||||
def part2():
|
||||
grid = toGrid(input_f)
|
||||
|
||||
@ -93,6 +116,7 @@ def part2():
|
||||
values = {}
|
||||
visited = []
|
||||
total_plots = []
|
||||
|
||||
for r,row in enumerate(grid):
|
||||
for c,col in enumerate(row):
|
||||
pos = (r,c)
|
||||
@ -100,7 +124,7 @@ def part2():
|
||||
current = get_value_in_direction(grid,pos)
|
||||
if pos not in visited:
|
||||
|
||||
x = get_neighbors(grid,pos,visited)
|
||||
x = get_neighbors_and_corners(grid,pos,visited)
|
||||
plot += x
|
||||
if pos not in plot:
|
||||
plot.append(pos)
|
||||
@ -108,24 +132,71 @@ def part2():
|
||||
values[current] = []
|
||||
|
||||
total_plots.append(plot)
|
||||
#pprint(total_plots)
|
||||
|
||||
for pdx,p in enumerate(total_plots):
|
||||
total_plots[pdx] = sorted(p)
|
||||
pprint(total_plots)
|
||||
|
||||
#directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||
directions = [(0, 1), (1, 0)]
|
||||
#directions = [(1, 1), (1, -1), (-1, -1), (-1, 1),(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||
|
||||
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
|
||||
result = 0
|
||||
|
||||
for v in total_plots:
|
||||
def find_edge(r,c,p,visited):
|
||||
edge = True
|
||||
end = ()
|
||||
for dr, dc in directions:
|
||||
neighbor = (r + dr, c + dc)
|
||||
if neighbor in p:
|
||||
if neighbor not in visited:
|
||||
#print((r,c),neighbor)
|
||||
visited.append((r,c))
|
||||
find_edge((r + dr),(c + dc),p,visited)
|
||||
else:
|
||||
#if (r+0,c+1) not in p or (r+1,c+0) not in p:
|
||||
edge = False
|
||||
end = (r,c)
|
||||
#print(r,c,(dr,dc))
|
||||
#input()
|
||||
if not edge:
|
||||
print(end)
|
||||
|
||||
sides = 0
|
||||
for x,y in v:
|
||||
for p in total_plots:
|
||||
for r,c in p:
|
||||
visited = []
|
||||
print('@',(r,c))
|
||||
|
||||
for dx, dy in directions:
|
||||
neighbor = (x + dx, y + dy) # Instead of finding all the neighbors, check if
|
||||
if neighbor in v:
|
||||
print(neighbor)
|
||||
sides += 1
|
||||
print()
|
||||
total_sides = len(v) * 4 - sides
|
||||
find_edge(r,c,p,visited)
|
||||
input()
|
||||
|
||||
result += (total_sides*len(v))
|
||||
# for v in total_plots:
|
||||
# total_corners = 0
|
||||
# corners = 0
|
||||
# for x,y in v:
|
||||
# print('Checking',(x,y))
|
||||
|
||||
# keep = True
|
||||
# for dx, dy in directions:
|
||||
# neighbor = (x + dx, y + dy) # Instead of finding all the neighbors, check if
|
||||
# if neighbor in v:
|
||||
|
||||
|
||||
# keep = False
|
||||
# #corners += 1
|
||||
# else:
|
||||
# corners += 1
|
||||
# #if corners < 2:
|
||||
# # total_corners += 1
|
||||
# print(corners)
|
||||
#input()
|
||||
#if corners == 3:
|
||||
# total_corners += 2
|
||||
#print(v,total_corners)
|
||||
|
||||
|
||||
#result += (total_corners*len(v))
|
||||
return result
|
||||
|
||||
start_time = time.time()
|
||||
|
Loading…
Reference in New Issue
Block a user