2024-12-14 10:46:57 +01:00
|
|
|
#!/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()
|
|
|
|
|
2024-12-14 19:53:08 +01:00
|
|
|
input_f = 'input'
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
def part1():
|
|
|
|
grid = toGrid(input_f)
|
2024-12-14 19:53:08 +01:00
|
|
|
#nprint(grid)
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
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'
|
2024-12-14 19:53:08 +01:00
|
|
|
|
2024-12-14 10:46:57 +01:00
|
|
|
if 'visible' in test.values():
|
|
|
|
visible.append((r,c))
|
|
|
|
return len(visible)
|
|
|
|
|
|
|
|
start_time = time.time()
|
2024-12-14 19:53:08 +01:00
|
|
|
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
def part2():
|
|
|
|
grid = toGrid(input_f)
|
2024-12-14 19:53:08 +01:00
|
|
|
#nprint(grid)
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
directions = {
|
|
|
|
'up': (-1, 0),
|
|
|
|
'left': (0, -1),
|
|
|
|
'right': (0, 1),
|
2024-12-14 19:53:08 +01:00
|
|
|
'down': (1, 0),
|
|
|
|
|
2024-12-14 10:46:57 +01:00
|
|
|
}
|
2024-12-14 19:53:08 +01:00
|
|
|
max_score = 0
|
|
|
|
best_view = ()
|
2024-12-14 10:46:57 +01:00
|
|
|
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
|
2024-12-14 19:53:08 +01:00
|
|
|
score = 1
|
|
|
|
|
2024-12-14 10:46:57 +01:00
|
|
|
view_distance = 0
|
|
|
|
(nr,nc) = (r,c)
|
2024-12-14 19:53:08 +01:00
|
|
|
#print((r,c),cur)
|
2024-12-14 10:46:57 +01:00
|
|
|
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):
|
2024-12-14 19:53:08 +01:00
|
|
|
#print(i)
|
|
|
|
if int(i) < int(cur):
|
|
|
|
view_distance = (idx+1)
|
2024-12-14 10:46:57 +01:00
|
|
|
if int(i) >= int(cur):
|
|
|
|
view_distance = (idx+1)
|
2024-12-14 19:53:08 +01:00
|
|
|
break
|
|
|
|
if idx == len(new)-1:
|
|
|
|
view_distance = (idx+1)
|
|
|
|
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
else:
|
2024-12-14 19:53:08 +01:00
|
|
|
view_distance = 1
|
|
|
|
score *= view_distance
|
|
|
|
if score > max_score:
|
|
|
|
max_score = score
|
|
|
|
best_view = (r,c)
|
|
|
|
|
2024-12-14 10:46:57 +01:00
|
|
|
|
2024-12-14 19:53:08 +01:00
|
|
|
#nprint(grid,best_view,get_value_in_direction(grid,best_view))
|
|
|
|
return max_score
|
2024-12-14 10:46:57 +01:00
|
|
|
|
|
|
|
start_time = time.time()
|
|
|
|
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|