#!/usr/bin/env python3 #(height, width) import random as rnd from enum import Enum import time DEBUG=True class SquareType(Enum): AISLE = 0 SEAT = 1 height=7 width=32 class GridSquare: #typ: SquareType, occupant: index def __init__(self, typ, occupant): self.typ = typ self.occupant = occupant def __str__(self): return "(%s|%s)" % (str(self.typ), str(self.occupant)) def __repr__(self): return str(self) def makeGrid(): return [[GridSquare(SquareType.AISLE, None) for x in range(0,width)] for y in range(0,height)] class Passenger: shuffled = 0 #dest and curr are both 2-tuples def __init__(self, dest, curr): self.dest = dest self.curr = curr def __str__(self): return "(%s|%s)" % (dest, curr) 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 i[0] >= 0 and i[1] >= 0 and i[0] < height and i[1] < width] def nextSquare(passenger, grid): #simple behaviour for now candidati = [] for i in mooreNeighbourhood(passenger.curr): if manhattanDistance(passenger.dest, i) < manhattanDistance(passenger.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 else: candidati.append(i) for i in candidati: if grid[i[0]][i[1]].typ == SquareType.AISLE and grid[i[0]][i[1]].occupant != None: #this should work well enough for one aisle plane return i #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 grid=makeGrid() #boarding order passengers = GenPassList(grid) # rnd.seed(605) # passengers.reverse() # rnd.shuffle(passengers) toad = 0 btime = 1 #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 global btime # print grid if DEBUG: for row in grid: for guy in row: if guy.occupant is not None: print(chr(0x4e00 + guy.occupant), end="") elif guy.typ == SquareType.SEAT: print(":", end="") else: 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 for (i, man) in enumerate(passengers): if man.curr == man.dest: continue if man.curr == (-1, -1): continue if i == 36 and DEBUG: print(nextSquare(man, grid)) done=False #im sexist nextS=nextSquare(man,grid) if passengers[i].shuffled == 0 and not ( grid[nextS[0]][nextS[1]].typ == SquareType.AISLE and grid[nextS[0]][nextS[1]].occupant is not None): cp = man.curr man.curr = nextS other = grid[cp[0]][cp[1]] other.occupant = grid[man.curr[0]][man.curr[1]].occupant #they move out of there if other.occupant is not None: passengers[other.occupant].curr = cp passengers[other.occupant].shuffled = 5 grid[man.curr[0]][man.curr[1]].occupant = i for man in passengers: if man.shuffled != 0: man.shuffled-=1 if DEBUG: time.sleep(0.01) os.system("clear") if done and toad >= len(passengers): print("Number of ticks: " + str(t)) exit() def run(): t = 0 while 1: tick(t) t += 1 run()