AdventOfCode/2024/12/solution.py

203 lines
5.6 KiB
Python

#!/bin/python3
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,grid_valid
start_time = time.time()
input_f = 'test'
#########################################
# #
# Part 1 #
# #
#########################################
def get_neighbors(grid,node,visited):
#print('@',node,' - Visited',visited)
directions = ['up','down','left','right']
offsets = {
'up': (-1, 0),
'down': (1, 0),
'left': (0, -1),
'right': (0, 1),
}
neighbors = []
for d in directions:
t = get_value_in_direction(grid,node)
if get_value_in_direction(grid,node,d) == t:
n = addTuples(offsets[d],node)
if n not in visited:
neighbors.append(n)
#print(n)
visited.append(n)
neighbors+=get_neighbors(grid,n,visited)
return neighbors
def part1():
grid = toGrid(input_f)
#nprint(grid)
values = {}
visited = []
total_plots = []
for r,row in enumerate(grid):
for c,col in enumerate(row):
pos = (r,c)
plot = []
current = get_value_in_direction(grid,pos)
if pos not in visited:
x = get_neighbors(grid,pos,visited)
plot += x
if pos not in plot:
plot.append(pos)
if current not in values:
values[current] = []
total_plots.append(plot)
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
result = 0
for v in total_plots:
sides = 0
for x,y in v:
for dx, dy in directions:
neighbor = (x + dx, y + dy)
if neighbor in v:
sides += 1
total_sides = len(v) * 4 - sides
result += (total_sides*len(v))
return result
start_time = time.time()
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)
#nprint(grid)
values = {}
visited = []
total_plots = []
for r,row in enumerate(grid):
for c,col in enumerate(row):
pos = (r,c)
plot = []
current = get_value_in_direction(grid,pos)
if pos not in visited:
x = get_neighbors_and_corners(grid,pos,visited)
plot += x
if pos not in plot:
plot.append(pos)
if current not in values:
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)]
result = 0
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)
for p in total_plots:
for r,c in p:
visited = []
print('@',(r,c))
find_edge(r,c,p,visited)
input()
# 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()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')