Skip to content

Instantly share code, notes, and snippets.

@prestonp
Created December 15, 2021 06:36
Show Gist options
  • Save prestonp/92117f52c9125b6dcc6acfbf891db98b to your computer and use it in GitHub Desktop.
Save prestonp/92117f52c9125b6dcc6acfbf891db98b to your computer and use it in GitHub Desktop.
advent of code 2021 day 15
# assuming positions are 2-tuple of row, column coordinates
def shortest_risk(cave, risk_total = 0, current_position)
if is_exit(cave, current_position):
return risk_total + cave[current_position]
new_total = risk_total + cave[current_position]
new_cave = mark_cave_visited(cave, current_position)
left = [current_position[0], current_position[1] - 1]
right = [current_position[0], current_position[1] + 1]
up [current_position[0] - 1, current_position[1]]
down = [current_position[0] + 1, current_position[1]]
candidates = []
if is_explorable(cave, left):
candidates.push(shortest_risk(new_cave, new_total, left))
if is_explorable(cave, right):
candidates.push(shortest_risk(new_cave, new_total, right))
if is_explorable(cave, up):
candidates.push(shortest_risk(new_cave, new_total, up))
if is_explorable(cave, down):
candidates.push(shortest_risk(new_cave, new_total, down))
if candidates.length == 0:
return 9999999 # some high value, we are at a dead end so we want to rule this path out
return min(candidates)
def is_explorable(cave, position):
return !already_visited(cave, position) and !out_of_bounds(cave, position)
def is_exit(cave, position):
# implement this
def out_of_bounds(cave, position):
# implement this
def mark_cave_visited(cave, current_position):
# return copy of cave but with the current_position set to some special value representing visited
def already_visited(cave, position):
# does this position == the special "visited" value?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment