AdventOfCode/2022/08/solution.py

155 lines
5.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 = 'test'
#########################################
# #
# 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'
# while notVisible:
# print((r,c),d,(nr,nc))
# new = get_value_in_direction(grid,(nr,nc),d)
# #print(new,(nr,nc))
# if new is not None:
# if cur > int(new) :
# print(cur,'>',new)
# notVisible = False
# (nr,nc) = addTuples((nr,nc),directions[d])
# if not grid_valid(nr,nc,grid):
# break
# nprint(grid,(r,c),str(cur),positions=visible)
# input()
#if not notVisible:
# visible.append((r,c))
#print((r,c),test)
#nprint(grid,(r,c),cur)
if 'visible' in test.values():
visible.append((r,c))
#print(x)
#input()
nprint(grid,positions=visible)
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),
'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
score = 0
view_distance = 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')
print('->>>',new)
if isinstance(new,list):
for idx,i in enumerate(new):
if int(i) >= int(cur):
print(i,cur,idx)
view_distance = (idx+1)
else:
if int(new) <= int(cur):
print(new,cur,1)
view_distance = +1
print('View distance',view_distance,d,'<---')
#if int(max(new)) >= int(cur):
# test[d] = 'hidden'
#else:
# test[d] = 'visible'
nprint(grid,(r,c),cur)
input()
if 'visible' in test.values():
visible.append((r,c))
nprint(grid,positions=visible)
return len(visible)
start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')