133 lines
4.2 KiB
Python
133 lines
4.2 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,nprint,get_value_in_direction,grid_valid,toGrid,addTuples
|
|
start_time = time.time()
|
|
|
|
input_f = 'input'
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
def part1():
|
|
grid = toGrid(input_f)
|
|
#nprint(grid)
|
|
|
|
directions = {
|
|
'up': (-1, 0),
|
|
'down': (1, 0),
|
|
'left': (0, -1),
|
|
'right': (0, 1),
|
|
}
|
|
|
|
visible = []
|
|
for r,row in enumerate(grid):
|
|
for c,col in enumerate(row):
|
|
if r == 0 or r == len(grid)-1 or c == 0 or c == len(row)-1:
|
|
visible.append((r,c))
|
|
else:
|
|
#print(r,c)
|
|
cur = get_value_in_direction(grid,(r,c))
|
|
|
|
x = []
|
|
test = {}
|
|
length = 0
|
|
notVisible = False
|
|
(nr,nc) = (r,c)
|
|
#print((r,c),cur)
|
|
for d in directions.keys():
|
|
#print(d)
|
|
if d == 'up':
|
|
length = r
|
|
if d == 'down':
|
|
length = len(grid)-r-1
|
|
if d == 'left':
|
|
length = c
|
|
if d == 'right':
|
|
length = len(row)-c-1
|
|
new = get_value_in_direction(grid,(nr,nc),d,length,'list')
|
|
|
|
if int(max(new)) >= int(cur):
|
|
test[d] = 'hidden'
|
|
else:
|
|
test[d] = 'visible'
|
|
|
|
if 'visible' in test.values():
|
|
visible.append((r,c))
|
|
return len(visible)
|
|
|
|
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)
|
|
|
|
directions = {
|
|
'up': (-1, 0),
|
|
'left': (0, -1),
|
|
'right': (0, 1),
|
|
'down': (1, 0),
|
|
|
|
}
|
|
max_score = 0
|
|
best_view = ()
|
|
visible = []
|
|
for r,row in enumerate(grid):
|
|
for c,col in enumerate(row):
|
|
if r == 0 or r == len(grid)-1 or c == 0 or c == len(row)-1:
|
|
visible.append((r,c))
|
|
else:
|
|
#print(r,c)
|
|
cur = get_value_in_direction(grid,(r,c))
|
|
|
|
length = 0
|
|
score = 1
|
|
|
|
view_distance = 0
|
|
(nr,nc) = (r,c)
|
|
#print((r,c),cur)
|
|
for d in directions.keys():
|
|
if d == 'up':
|
|
length = r
|
|
if d == 'down':
|
|
length = len(grid)-r-1
|
|
if d == 'left':
|
|
length = c
|
|
if d == 'right':
|
|
length = len(row)-c-1
|
|
new = get_value_in_direction(grid,(nr,nc),d,length,'list')
|
|
if isinstance(new,list):
|
|
for idx,i in enumerate(new):
|
|
#print(i)
|
|
if int(i) < int(cur):
|
|
view_distance = (idx+1)
|
|
if int(i) >= int(cur):
|
|
view_distance = (idx+1)
|
|
break
|
|
if idx == len(new)-1:
|
|
view_distance = (idx+1)
|
|
|
|
|
|
else:
|
|
view_distance = 1
|
|
score *= view_distance
|
|
if score > max_score:
|
|
max_score = score
|
|
best_view = (r,c)
|
|
|
|
|
|
#nprint(grid,best_view,get_value_in_direction(grid,best_view))
|
|
return max_score
|
|
|
|
start_time = time.time()
|
|
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') |