Skip to content

Commit

Permalink
refactor for dry
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Buhr authored and Ray Buhr committed Dec 8, 2022
1 parent b0d5da0 commit 228e265
Showing 1 changed file with 15 additions and 52 deletions.
67 changes: 15 additions & 52 deletions 2022/day08.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,59 +49,22 @@ def find_all_visible(grid):
print(f"part a {find_all_visible(grid):.0f}")


def look_left(grid, row, col):
h = grid[row, col]
see = 0
while True:
col -= 1
if col < 0:
break
if grid[row, col] < h:
see += 1
else:
see += 1
break
return see if see > 0 else 1


def look_right(grid, row, col):
h = grid[row, col]
L = len(grid)
see = 0
while True:
col += 1
if col >= L:
break
if grid[row, col] < h:
see += 1
else:
see += 1
break
return see if see > 0 else 1


def look_up(grid, row, col):
h = grid[row, col]
see = 0
while True:
row -= 1
if row < 0:
break
if grid[row, col] < h:
see += 1
else:
see += 1
break
return see if see > 0 else 1
def move(direction, row, col):
m = {
"left": (row, col - 1),
"right": (row, col + 1),
"up": (row - 1, col),
"down": (row + 1, col),
}
return m[direction]


def look_down(grid, row, col):
def look(direction, grid, row, col):
h = grid[row, col]
L = len(grid)
see = 0
while True:
row += 1
if row >= L:
row, col = move(direction, row, col)
if col < 0 or col >= len(grid) or row < 0 or row >= len(grid):
break
if grid[row, col] < h:
see += 1
Expand All @@ -112,10 +75,10 @@ def look_down(grid, row, col):


def calc_scenic_score(grid, row, col):
up = look_up(grid, row, col)
down = look_down(grid, row, col)
left = look_left(grid, row, col)
right = look_right(grid, row, col)
up = look("up", grid, row, col)
down = look("down", grid, row, col)
left = look("left", grid, row, col)
right = look("right", grid, row, col)
return up * down * left * right


Expand Down

0 comments on commit 228e265

Please sign in to comment.