Skip to content

Instantly share code, notes, and snippets.

@LemonInTheDark
Created January 12, 2022 05:50
Show Gist options
  • Select an option

  • Save LemonInTheDark/85675c84692e7108b208d61eb9a64c21 to your computer and use it in GitHub Desktop.

Select an option

Save LemonInTheDark/85675c84692e7108b208d61eb9a64c21 to your computer and use it in GitHub Desktop.
Python implementation of share(), both versions
import math
big_grid = [
[1000, 0, 0, 0, 0],
[0, 0, "S", 0, 0],
["S", 0, 0, 0, 200,]
]
small_base = [0, 0, 0, 0, 0]
small_grid = [
small_base.copy(),
small_base.copy(),
[0, 0, 100, 0, 0],
small_base.copy(),
small_base.copy(),
]
grid = big_grid
created_grid = []
timer = 0
use_better = False
def init_cells():
living_grid = created_grid
living_grid.clear()
for row in grid:
new_row = []
living_grid += [new_row]
for cell in row:
solid = False
space = False
amount = 0
if isinstance(cell, str):
if cell == "X":
solid = True
elif cell == "S":
space = True
elif cell < 0:
solid = True
else:
amount = max(cell, 0)
tile = Tile(solid, space, amount)
new_row += [tile]
build_neighbors(living_grid)
display_grid(0)
def build_neighbors(grid):
for y in range(0, len(grid)):
row = grid[y]
for x in range(0, len(row)):
cell = row[x]
cell.set_adjacents(grid, x, y)
def display_grid(time):
print("-" * len(created_grid[0]))
total_halves(time)
for row in created_grid:
row_str = ""
for cell in row:
row_str += f"[{cell.get_display()}] "
print(row_str)
def share_grid(timer):
timer += 1
for row in created_grid:
for cell in row:
cell.process(timer)
display_grid(timer)
return timer
def total_grid():
total = 0
for row in created_grid:
for cell in row:
total += cell.amount
print(f"Current total {total}")
def total_halves(time):
sum = 0
top = 0
bottom = 0
halfway = math.floor(len(created_grid) / 2)
all_same = True
same = round(created_grid[0][0].amount, 2)
for y in range(0, len(created_grid)):
total = 0
for cell in created_grid[y]:
if cell.solid:
continue
total += cell.amount
if not same == round(cell.amount, 2):
all_same = False
sum += total
if y == halfway:
continue
if y < halfway:
top += total
else:
bottom += total
ratio = 0
if bottom:
ratio = top / bottom
print(f"Time {time}, Midrow {halfway}, All Same {all_same}, Current total {round(sum)}, Top {round(top)}, Bottom {round(bottom)}, Ratio {round(ratio, 3)}")
class Tile:
def __init__(self, solid, space, amount):
self.solid = solid
self.space = space
self.amount = amount
self.archive = 0
self.archived_tick = 0
self.last_tick = 0
self.adjacents = []
self.share_ratio = 0
def get_display(self):
output = ""
if self.solid:
output = "X "
else:
#output = str(len(self.adjacents))
output = str(round(self.amount, 2))
#output = str(self.share_ratio)
return output
def process(self, timer):
self.archive_self(timer)
self.last_tick = timer
adjacent_count = len(self.adjacents)
for cell in self.adjacents:
if cell.last_tick == timer:
continue
cell.archive_self(timer)
if use_better:
self.better_share(cell)
else:
self.share(cell, adjacent_count)
def share(self, other, adjacent_count):
if self.space:
other.amount = self.amount
return
if other.space:
other.share(self, len(other.adjacents))
return
delta = (self.archive - other.archive) / (adjacent_count + 1)
self.amount -= delta
other.amount += delta
def better_share(self, other):
if self.space:
other.amount = self.amount
return
if other.space:
other.share(self, len(other.adjacents))
return
delta = self.archive - other.archive
if delta > 0:
delta *= self.share_ratio
else:
delta *= other.share_ratio
self.amount -= delta
other.amount += delta
if self.amount < 0 or other.amount < 0:
print("We have moles < 0")
# raise Exception
def archive_self(self, timer):
if self.archived_tick != timer:
self.archived_tick = timer
self.archive = self.amount
def add_adjacent(self, grid, pos_x, pos_y):
if len(grid) <= pos_y or pos_y < 0:
return
row = grid[pos_y]
if len(row) <= pos_x or pos_x < 0:
return
cell = row[pos_x]
if cell.solid or self.solid:
return
self.adjacents += [cell]
def set_adjacents(self, grid, pos_x, pos_y):
self.add_adjacent(grid, pos_x - 1, pos_y)
self.add_adjacent(grid, pos_x + 1, pos_y)
self.add_adjacent(grid, pos_x, pos_y - 1)
self.add_adjacent(grid, pos_x, pos_y + 1)
self.set_share_ratio()
def set_share_ratio(self):
self.share_ratio = 1 / (1 + len(self.adjacents))
init_cells()
use_better = True
for i in range(1, 100):
timer = share_grid(timer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment