Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

It's incredibly hideous, inefficient, and it took me hours to isolate the typos I had made but it's finally done just in time for Day 9

Part 1:

forest = []

visibleTrees = []

visibleTotal = 0

with open('input', 'r') as f:

    for line in f:

        newArr = []

        newBools = []

        line=line.strip()

        for tree in line:

            newArr.append(int(tree))

            newBools.append(False)

        forest.append(newArr)

        visibleTrees.append(newBools)

    height = len(forest)

    width = len(forest[0])

    # for y in range(height):

    #     print()

    #     for x in range(width):

    #         print(forest[y][x], end=' ')

    # Right to Left and Left to Right: 

    for rowidx, treeRow in enumerate(forest):

        for treeIdx, tree in enumerate(treeRow):

            if treeIdx is 0 or treeIdx is len(treeRow) - 1:

                visibleTrees[rowidx][treeIdx] = True

                continue

            if max(forest[rowidx][:treeIdx]) < tree or max(forest[rowidx][treeIdx+1:]) < tree:

                visibleTrees[rowidx][treeIdx] = True

    print('\nBefore')

    # Top Down

    for i in reversed(range(len(forest[0]))):

        tallest = -1

        for j in range(len(forest)):

            # print('Loop 1: ', forest[i][j])

            if forest[j][i] > tallest: 

                visibleTrees[j][i] = True

                tallest = forest[j][i]

    print()

    for col in reversed(range(len(forest[0]))):

        tallest = -1

        for row in reversed(range(len(forest))):

            # print('Loop 2: ', forest[col][row])

            if forest[row][col] > tallest: 

                visibleTrees[row][col] = True

                tallest = forest[row][col]

print('\nAfter')

for y in range(height):

    print()

    for x in range(width):

        print(forest[y][x], end='')

        print(visibleTrees[y][x], end=' ')

print()

for xIdx in range(len(visibleTrees)):

    for yIdx in range(len(visibleTrees[0])):

        if visibleTrees[xIdx][yIdx]:

            visibleTotal += 1

print(visibleTotal)

Part 2:

forest = []

visibleTrees = []

maxDistance = 0

with open('input', 'r') as f:

    for line in f:

        newArr = []

        newBools = []

        line = line.strip()

        for tree in line:

            newArr.append(int(tree))

            newBools.append(False)

        forest.append(newArr)

        visibleTrees.append(newBools)

    height = len(forest)

    width = len(forest[0])

for row in range(len(forest)):

    for col in range(len(forest[row])):

        north = 0

        south = 0

        east = 0

        west = 0

        if row is len(forest) - 1:

            south = 0

        if row is 0:

            north = 0

        if col is len(forest[row]) - 1:

            east = 0

        if col is 0:

            west = 0

        maxTree = forest[row][col]

        # if south is not 0:

        for rowNum in range(row, len(forest)):

            if row is 3 and col is 2:

                print('south', rowNum)

            if row is rowNum:

                continue

            elif forest[rowNum][col] < maxTree:

                south += 1

                if row is 3 and col is 2:

                    print('incremented', south)

            else:

                south +=1 

                break

        # if north is not 0:

        for rowNum in reversed(range(0, row)):

            if row is 3 and col is 2:

                print('north', rowNum)

            if row is rowNum:

                continue

            elif forest[rowNum][col] < maxTree:

                north += 1

                if row is 3 and col is 2:

                    print('incremented', north)

            else:

                north +=1 

                break

        # if east is not 0:

        for colNum in range(col, len(forest[row])):

            if row is 3 and col is 2:

                print('east', colNum)

            if col is colNum:

                continue

            elif forest[row][colNum] < maxTree:

                east += 1

                if row is 3 and col is 2:

                    print('incremented', east)

            else:

                east +=1 

                break

        # if west is not 0:

        for colNum in reversed(range(0, col)):

            if row is 3 and col is 2:

                print('west', colNum)

            if col is colNum:

                continue

            elif forest[row][colNum] < maxTree:

                west += 1

                if row is 3 and col is 2:

                    print('incremented', west)

            else:

                west +=1 

                break

        curDistance = north*south*east*west

        if maxDistance < curDistance:

            print('Things changed', row, col)

            maxrow = row

            maxcol = col

            maxnorth = north

            # print('north', north)

            maxsouth = south

            # print('south', south)

            maxeast = east

            # print('east', east)

            maxwest = west

            # print('west', west)

            maxDistance = curDistance

print(forest[3][2])

for y in range(height):

    print()

    for x in (range(width)):

        print(forest[y][x], end=' ')


print(maxDistance)
Jump in the discussion.

No email address required.

Posts like this is why I do Heroine.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.