print fun
This commit is contained in:
parent
b1ca2d049f
commit
2918a33ca3
@ -7,6 +7,12 @@ criteria = lambda _cur, _neighbor: _neighbor - _cur <= 1
|
||||
|
||||
|
||||
def build_graph(mx):
|
||||
"""
|
||||
This is the meat of the solution,
|
||||
getting the valid neighbors - and then weighing those neighbors as pathable
|
||||
- loop through each row/col and then find who that cell can navigate to, ULDR
|
||||
- if it can navigate, add an edge
|
||||
"""
|
||||
graph = Graph()
|
||||
for y, row in enumerate(mx):
|
||||
for x, _ in enumerate(row):
|
||||
@ -19,39 +25,50 @@ def build_graph(mx):
|
||||
def part1(mx):
|
||||
start = matrix.find_in_matrix(mx, "S")
|
||||
end = matrix.find_in_matrix(mx, "E")
|
||||
# Now that we got the start/end, fix those to 'a' and 'z' like the instructions said
|
||||
mx[start[0]][start[1]] = "a"
|
||||
mx[end[0]][end[1]] = "z"
|
||||
# Turn the a-z into numbers 0-25
|
||||
matrix.apply_to_all(mx, lambda x: ord(x) - ord("a"))
|
||||
|
||||
graph = build_graph(mx)
|
||||
path = find_path(graph, start, end)
|
||||
print(len(path.nodes))
|
||||
print(len(path.nodes) -1)
|
||||
|
||||
|
||||
def part2(mx):
|
||||
end = matrix.find_in_matrix(mx, "E")
|
||||
# don't NEED this, because we go from all 'a', but need to replace it still
|
||||
s = matrix.find_in_matrix(mx, "S")
|
||||
# Now that we got the start/end, fix those to 'a' and 'z' like the instructions said
|
||||
mx[s[0]][s[1]] = "a"
|
||||
mx[end[0]][end[1]] = "z"
|
||||
|
||||
starts = matrix.find_in_matrix(mx, "a", one=False)
|
||||
|
||||
# Turn the a-z into numbers 0-25
|
||||
matrix.apply_to_all(mx, lambda x: ord(x) - ord("a"))
|
||||
graph = build_graph(mx)
|
||||
|
||||
n_counts = []
|
||||
paths = []
|
||||
# Find the yx of all 'a'
|
||||
for start in starts:
|
||||
try:
|
||||
path = find_path(graph, start, end)
|
||||
n_counts.append(len(path.nodes) - 1)
|
||||
paths.append(path)
|
||||
except algorithm.NoPathError:
|
||||
#print('x', end='')
|
||||
# This 'a' is in a puddle of 'c's, so it's not pathable
|
||||
pass
|
||||
print(n_counts)
|
||||
print(min(n_counts))
|
||||
shortest = min(paths, key= lambda x: x.total_cost)
|
||||
matrix.apply_to_all(mx, lambda x: chr(x+ord('a')))
|
||||
matrix.highlight(mx, red=shortest.nodes)
|
||||
print(shortest.total_cost)
|
||||
|
||||
|
||||
def main():
|
||||
mx = matrix.load_matrix_file(shared.get_fname(12), matrix.split_word_to_chr_list)
|
||||
# part1(mx)
|
||||
part1(mx)
|
||||
mx = matrix.load_matrix_file(shared.get_fname(12), matrix.split_word_to_chr_list)
|
||||
part2(mx)
|
||||
|
||||
|
||||
|
@ -236,7 +236,7 @@ def pmx(*matrices, pad=True, space=True):
|
||||
print("".join([f(x) for x in c]))
|
||||
|
||||
|
||||
def ppmx(*matrices, pad=True, space=True):
|
||||
def ppmx(*matrices, pad=True, space=True, zero='.'):
|
||||
"""
|
||||
print a matrix of anything, Falsy values turns to `.` for clarity
|
||||
"""
|
||||
@ -245,19 +245,19 @@ def ppmx(*matrices, pad=True, space=True):
|
||||
for row in matrices:
|
||||
r = []
|
||||
for col in row:
|
||||
r.append("".join([f"{x or '.'}".rjust(3) for x in col]))
|
||||
r.append("".join([f"{x or zero}".rjust(3) for x in col]))
|
||||
print(" ".join(r))
|
||||
else:
|
||||
for row in matrices:
|
||||
for c in row:
|
||||
if pad:
|
||||
f = lambda x: f"{x or '.'}".rjust(2)
|
||||
f = lambda x: f"{x or zero}".rjust(2)
|
||||
if space:
|
||||
f = lambda x: f"{x or '.'}".rjust(3)
|
||||
f = lambda x: f"{x or zero}".rjust(3)
|
||||
else:
|
||||
f = lambda x: f"{x or '.'}"
|
||||
f = lambda x: f"{x or zero}"
|
||||
if space:
|
||||
f = lambda x: f"{x or '.'} "
|
||||
f = lambda x: f"{x or zero} "
|
||||
print("".join([f(x) for x in c]))
|
||||
|
||||
|
||||
@ -278,4 +278,4 @@ def highlight(matrix, red=[], green=[], blue=[]):
|
||||
for (y, x) in blue:
|
||||
new = f"{colors.BLUE}{mx[y][x]}{colors.ENDC}"
|
||||
mx[y][x] = new
|
||||
ppmx(mx, pad=False, space=False)
|
||||
ppmx(mx, pad=False, space=True, zero='0')
|
||||
|
Loading…
Reference in New Issue
Block a user