Solved 2022/08 P2

This commit is contained in:
FrederikBaerentsen 2024-12-14 19:53:08 +01:00
parent e1a780e728
commit d7706dddf6
2 changed files with 36 additions and 54 deletions

View File

@ -56,8 +56,6 @@ Consider your map; *how many trees are visible from outside the grid?*
Your puzzle answer was `1690`. Your puzzle answer was `1690`.
The first half of this puzzle is complete! It provides one gold star: \*
## \-\-- Part Two \-\-- {#part2} ## \-\-- Part Two \-\-- {#part2}
Content with the amount of tree cover available, the Elves just need to Content with the amount of tree cover available, the Elves just need to
@ -118,8 +116,14 @@ spot for the tree house.
Consider each tree on your map. *What is the highest scenic score Consider each tree on your map. *What is the highest scenic score
possible for any tree?* possible for any tree?*
Answer: Your puzzle answer was `535680`.
Although it hasn\'t changed, you can still [get your puzzle Both parts of this puzzle are complete! They provide two gold stars:
\*\*
At this point, you should [return to your Advent calendar](/2022) and
try another puzzle.
If you still want to see it, you can [get your puzzle
input](8/input). input](8/input).

View File

@ -5,7 +5,7 @@ sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile,nprint,get_value_in_direction,grid_valid,toGrid,addTuples from fred import list2int,get_re,nprint,lprint,loadFile,nprint,get_value_in_direction,grid_valid,toGrid,addTuples
start_time = time.time() start_time = time.time()
input_f = 'test' input_f = 'input'
######################################### #########################################
# # # #
@ -14,7 +14,7 @@ input_f = 'test'
######################################### #########################################
def part1(): def part1():
grid = toGrid(input_f) grid = toGrid(input_f)
nprint(grid) #nprint(grid)
directions = { directions = {
'up': (-1, 0), 'up': (-1, 0),
@ -54,33 +54,13 @@ def part1():
test[d] = 'hidden' test[d] = 'hidden'
else: else:
test[d] = 'visible' 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(): if 'visible' in test.values():
visible.append((r,c)) visible.append((r,c))
#print(x)
#input()
nprint(grid,positions=visible)
return len(visible) return len(visible)
start_time = time.time() start_time = time.time()
#print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
######################################### #########################################
@ -90,15 +70,17 @@ start_time = time.time()
######################################### #########################################
def part2(): def part2():
grid = toGrid(input_f) grid = toGrid(input_f)
nprint(grid) #nprint(grid)
directions = { directions = {
'up': (-1, 0), 'up': (-1, 0),
'down': (1, 0),
'left': (0, -1), 'left': (0, -1),
'right': (0, 1), 'right': (0, 1),
} 'down': (1, 0),
}
max_score = 0
best_view = ()
visible = [] visible = []
for r,row in enumerate(grid): for r,row in enumerate(grid):
for c,col in enumerate(row): for c,col in enumerate(row):
@ -108,16 +90,13 @@ def part2():
#print(r,c) #print(r,c)
cur = get_value_in_direction(grid,(r,c)) cur = get_value_in_direction(grid,(r,c))
x = []
test = {}
length = 0 length = 0
score = 0 score = 1
view_distance = 0 view_distance = 0
notVisible = False
(nr,nc) = (r,c) (nr,nc) = (r,c)
print((r,c),cur) #print((r,c),cur)
for d in directions.keys(): for d in directions.keys():
#print(d)
if d == 'up': if d == 'up':
length = r length = r
if d == 'down': if d == 'down':
@ -127,29 +106,28 @@ def part2():
if d == 'right': if d == 'right':
length = len(row)-c-1 length = len(row)-c-1
new = get_value_in_direction(grid,(nr,nc),d,length,'list') new = get_value_in_direction(grid,(nr,nc),d,length,'list')
print('->>>',new)
if isinstance(new,list): if isinstance(new,list):
for idx,i in enumerate(new): for idx,i in enumerate(new):
#print(i)
if int(i) < int(cur):
view_distance = (idx+1)
if int(i) >= int(cur): if int(i) >= int(cur):
print(i,cur,idx) view_distance = (idx+1)
break
if idx == len(new)-1:
view_distance = (idx+1) 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) else:
return len(visible) 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() start_time = time.time()
print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms')