Dice simulator

This commit is contained in:
Jan-Niclas Loosen
2025-10-24 20:58:31 +02:00
parent 49438ccaaf
commit 90ce78f17d
9 changed files with 312 additions and 6 deletions

View File

@@ -10,28 +10,34 @@ max_y = len(data)
max_x = len(data[0])
start = (max_y-1, max_x-1)
# Cache with size of n points
cache = [[None for _ in range(max_x)] for _ in range(max_y)]
# Table with size of data
table = [[None for _ in range(max_x)] for _ in range(max_y)]
def find_path(y, x):
# Return bad value for positions which are out of bounds
if not (0 <= y < max_y and 0 <= x < max_x):
return (), float('-inf')
if cache[y][x] is not None:
return cache[y][x]
# Table look up
if table[y][x] is not None:
return table[y][x]
# End of the recursion is the starting position
if y == 0 and x == 0:
res = (((y, x),), data[y][x])
# Calculate defined recursion on all other existing positions
else:
west_path, west_val = find_path(y, x-1)
north_path, north_val = find_path(y-1, x)
# Choose most valuable path to the considered position
if west_val > north_val:
res = (west_path + ((y, x),), west_val + data[y][x])
else:
res = (north_path + ((y, x),), north_val + data[y][x])
cache[y][x] = res
# Store most valuable path in the table and return
table[y][x] = res
return res
path, val = find_path(*start)