From cb444f89469574cf47373ec3ba4590d453f1510e Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Thu, 9 Apr 2020 02:14:55 +0200 Subject: [PATCH] Finally finished, learned how not init nested array --- 15/main.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/15/main.py b/15/main.py index 4509c59..2c35286 100644 --- a/15/main.py +++ b/15/main.py @@ -4,29 +4,31 @@ import numpy as np # Given a grid with start in one corner and goal in opposite corner, find -# the number of paths you can take from start to goal +# the number of paths you can take from start to goal. Do this for a grid +# of 20 by 20 -gs = [[0] * 21] * 21 - -def gridRoutes(x, y): +def gridRoutes(x, y, gs): res = 0 if gs[x][y] > 0: res = gs[x][y] else: - if x == 1: - res = y + 1 - elif y == 1: + if y > x: + h = x + x = y + y = h + if y == 1: res = x + 1 else: - res = gridRoutes(x - 1, y) + gridRoutes(x, y - 1) + res = gridRoutes(x - 1, y, gs) + gridRoutes(x, y - 1, gs) gs[x][y] = res - print(x, y, res) + # print(x, y, res) return res + def dumbGridRoutes(x, y): if x == 1: return y + 1 @@ -39,9 +41,14 @@ def dumbGridRoutes(x, y): def main(): print("Hello, this is Patrick") - g = gridRoutes(3, 2) - gd = dumbGridRoutes(3, 2) - print(g, gd) + gss = [0] * 21 + for i in range(21): + gss[i] = [0] * 21 + + g = gridRoutes(20, 20, gss) + # dg = dumbGridRoutes(3, 2) + print(g) + # print(gss) if __name__ == "__main__": main() \ No newline at end of file