|
|
@ -1,17 +1,24 @@ |
|
|
|
#!/usr/bin/env python3 |
|
|
|
#(height, width) |
|
|
|
import random as rnd |
|
|
|
import os |
|
|
|
from enum import Enum |
|
|
|
import time |
|
|
|
import math |
|
|
|
|
|
|
|
DEBUG=True |
|
|
|
|
|
|
|
class SquareType(Enum): |
|
|
|
AISLE = 0 |
|
|
|
SEAT = 1 |
|
|
|
WALL = 2 |
|
|
|
|
|
|
|
height=0 |
|
|
|
width=0 |
|
|
|
|
|
|
|
height=7 |
|
|
|
width=32 |
|
|
|
grid=[] |
|
|
|
# entries=[(3,0)] |
|
|
|
entries=[(0,3)] |
|
|
|
|
|
|
|
class GridSquare: |
|
|
|
#typ: SquareType, occupant: index |
|
|
@ -28,6 +35,7 @@ def makeGrid(): |
|
|
|
|
|
|
|
class Passenger: |
|
|
|
shuffled = 0 |
|
|
|
inter = None |
|
|
|
#dest and curr are both 2-tuples |
|
|
|
def __init__(self, dest, curr): |
|
|
|
self.dest = dest |
|
|
@ -39,14 +47,19 @@ def manhattanDistance(pos1, pos2): |
|
|
|
return abs(pos1[0] - pos2[0]) + abs(pos1[1] - pos2[1]) |
|
|
|
|
|
|
|
def mooreNeighbourhood(pos): |
|
|
|
return [i for i in [(pos[0], pos[1]+1), (pos[0]+1, pos[1]), (pos[0]-1, pos[1]), (pos[0], pos[1]-1)] if |
|
|
|
return [i for i in [(pos[0]+1, pos[1]), (pos[0], pos[1]+1), (pos[0]-1, pos[1]), (pos[0], pos[1]-1)] if |
|
|
|
i[0] >= 0 and i[1] >= 0 and i[0] < height and i[1] < width] |
|
|
|
|
|
|
|
def nextSquare(passenger, grid): |
|
|
|
#simple behaviour for now |
|
|
|
candidati = [] |
|
|
|
dest = None |
|
|
|
if passenger.inter == None: |
|
|
|
dest = passenger.dest |
|
|
|
else: |
|
|
|
dest = passenger.inter |
|
|
|
for i in mooreNeighbourhood(passenger.curr): |
|
|
|
if manhattanDistance(passenger.dest, i) < manhattanDistance(passenger.dest, passenger.curr): |
|
|
|
if manhattanDistance(dest, i) < manhattanDistance(dest, passenger.curr): |
|
|
|
# if grid[passenger.curr[0]][passenger.curr[1]].typ == SquareType.SEAT and i[1] != passenger.curr[1] and False: |
|
|
|
if grid[passenger.curr[0]][passenger.curr[1]].typ == SquareType.SEAT and i[1] != passenger.curr[1]: |
|
|
|
pass |
|
|
@ -58,37 +71,55 @@ def nextSquare(passenger, grid): |
|
|
|
#todo: implement shufflin' |
|
|
|
return candidati[0] |
|
|
|
|
|
|
|
def GenPassList(grid): |
|
|
|
border = [Passenger((a, 0),(-1,-1)) for a in range(4,7)] |
|
|
|
for a in range(4,7): |
|
|
|
grid[a][0].typ = SquareType.SEAT |
|
|
|
for x in range(1,width): |
|
|
|
border += [Passenger((a, x),(-1,-1)) for a in range(0,3)] |
|
|
|
for a in range(0,3): |
|
|
|
grid[a][x].typ = SquareType.SEAT |
|
|
|
border += reversed([Passenger((a, x),(-1,-1)) for a in range(4,7)]) |
|
|
|
# border += [Passenger((a, x),(-1,-1)) for a in range(4,7)] |
|
|
|
for a in range(4,7): |
|
|
|
grid[a][x].typ = SquareType.SEAT |
|
|
|
return border |
|
|
|
def GenPassList(): |
|
|
|
global height |
|
|
|
global width |
|
|
|
global grid |
|
|
|
border=[] |
|
|
|
arrFile = open("figure2.txt") |
|
|
|
|
|
|
|
height=int(arrFile.readline()) |
|
|
|
width=int(arrFile.readline()) |
|
|
|
print(height) |
|
|
|
print(width) |
|
|
|
|
|
|
|
grid = [[GridSquare(SquareType.AISLE, None) for x in range(0,width)] for y in range(0,height)] |
|
|
|
|
|
|
|
for y in range(0,height): |
|
|
|
for x in range(0,width+1): |
|
|
|
char = arrFile.read(1); |
|
|
|
match char: |
|
|
|
case 'S': |
|
|
|
grid[y][x].typ = SquareType.SEAT |
|
|
|
border.append(Passenger((y,x),(-1,-1))) |
|
|
|
case 'W': |
|
|
|
grid[y][x].typ = SquareType.WALL |
|
|
|
case '.': |
|
|
|
grid[y][x].typ = SquareType.AISLE |
|
|
|
|
|
|
|
for x in border: |
|
|
|
for i in range(1,7): |
|
|
|
inc = (-1)**i * -math.floor((i+1)/2) |
|
|
|
if (inc + x.dest[0] > 0) and (inc + x.dest[0] < height) and (grid[x.dest[0]+inc][x.dest[1]].typ == SquareType.AISLE): |
|
|
|
x.inter=(inc + x.dest[0], x.dest[1]) |
|
|
|
|
|
|
|
grid=makeGrid() |
|
|
|
return border |
|
|
|
|
|
|
|
#boarding order |
|
|
|
passengers = GenPassList(grid) |
|
|
|
passengers = GenPassList() |
|
|
|
|
|
|
|
# rnd.seed(605) |
|
|
|
# passengers.reverse() |
|
|
|
# rnd.shuffle(passengers) |
|
|
|
rnd.shuffle(passengers) |
|
|
|
toad = 0 |
|
|
|
btime = 1 #passengers board every btime ticks |
|
|
|
btime = 3 #passengers board every btime ticks |
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
def nunty(x): |
|
|
|
if x is None: return -1 |
|
|
|
return x |
|
|
|
|
|
|
|
def tick(t): |
|
|
|
done=True |
|
|
|
global toad |
|
|
@ -98,25 +129,33 @@ def tick(t): |
|
|
|
for row in grid: |
|
|
|
for guy in row: |
|
|
|
if guy.occupant is not None: |
|
|
|
print(chr(0x4e00 + guy.occupant), end="") |
|
|
|
print(chr(0x20000 + guy.occupant), end="") |
|
|
|
elif guy.typ == SquareType.SEAT: |
|
|
|
print(":", end="") |
|
|
|
elif guy.typ == SquareType.WALL: |
|
|
|
print("W", end="") |
|
|
|
else: |
|
|
|
print("。", end="") |
|
|
|
print("_", end="") |
|
|
|
print() |
|
|
|
print("---") |
|
|
|
print(grid[3][0].occupant, grid[3][1].occupant, passengers[nunty(grid[3][1].occupant)].dest) |
|
|
|
if toad < len(passengers) and grid[3][0].occupant is None and t % btime == 0: |
|
|
|
grid[3][0].occupant = toad #added |
|
|
|
passengers[toad].curr = (3, 0) |
|
|
|
toad += 1 |
|
|
|
# print(grid[3][0].occupant, grid[3][1].occupant, passengers[nunty(grid[3][1].occupant)].dest) |
|
|
|
if t % btime == 0: |
|
|
|
for x in entries: |
|
|
|
if toad < len(passengers) and grid[x[0]][x[1]].occupant is None: |
|
|
|
grid[x[0]][x[1]].occupant = toad #added |
|
|
|
passengers[toad].curr = (x[0], x[1]) |
|
|
|
toad += 1 |
|
|
|
for (i, man) in enumerate(passengers): |
|
|
|
if man.curr == man.dest: |
|
|
|
continue |
|
|
|
if man.inter is not None and man.curr == man.inter: |
|
|
|
man.shuffled = 3 |
|
|
|
man.inter = None |
|
|
|
if man.curr == (-1, -1): |
|
|
|
continue |
|
|
|
if i == 36 and DEBUG: |
|
|
|
print(nextSquare(man, grid)) |
|
|
|
# if i == 36 and DEBUG: |
|
|
|
# print(nextSquare(man, grid)) |
|
|
|
print(str(man.curr) + ", " + str(man.inter) + ", " + str(man.dest)) |
|
|
|
done=False |
|
|
|
#im sexist |
|
|
|
nextS=nextSquare(man,grid) |
|
|
@ -134,6 +173,7 @@ def tick(t): |
|
|
|
man.shuffled-=1 |
|
|
|
if DEBUG: |
|
|
|
time.sleep(0.01) |
|
|
|
# time.sleep(0.05) |
|
|
|
os.system("clear") |
|
|
|
if done and toad >= len(passengers): |
|
|
|
print("Number of ticks: " + str(t)) |
|
|
|