132 lines
3.6 KiB
Python
132 lines
3.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
|
|
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 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(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) # Instead of finding all the neighbors, check if
|
|
if neighbor in v:
|
|
print(neighbor)
|
|
sides += 1
|
|
print()
|
|
total_sides = len(v) * 4 - sides
|
|
|
|
result += (total_sides*len(v))
|
|
return result
|
|
|
|
start_time = time.time()
|
|
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') |