Browse Source

Added reading from file, moving to stow luggage before sitting

master
iamsosmart19 3 years ago
parent
commit
0cde427aed
  1. 9
      figure1.txt
  2. 30
      figure2.txt
  3. 3
      format.txt
  4. 102
      main.py

9
figure1.txt

@ -0,0 +1,9 @@
7
32
.SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
.SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
.SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
................................
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS

30
figure2.txt

@ -0,0 +1,30 @@
28
15
WWW.SSSSSSSSSSS
WWW.SSSSSSSSSSS
WWW.SSSSSSSSSSS
...............
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
...............
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
...............
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
.SSSSSSSSSSSSSS
...............
WWW.SSSSSSSSSSS
WWW.SSSSSSSSSSS
WWW.SSSSSSSSSSS

3
format.txt

@ -0,0 +1,3 @@
height
width
ARRANGEMENT

102
main.py

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

Loading…
Cancel
Save