from google.colab import drive drive.mount('/content/drive') from math import sqrt import matplotlib.pyplot as plt import numpy as np import os File_Directory = 'drive/MyDrive/Ecole/PolyHash2022/DataSet/' File_Name = 'a_an_example.in.txt' File_Path = File_Directory+File_Name DataSet = [line.strip().split() for line in open(File_Path, "r")] Score:int = 0 def is_number(s): try: float(s) return True except ValueError: pass try: import unicodedata unicodedata.numeric(s) return True except (TypeError,ValueError): pass return False def convertToFloat(DataSet): for i in range(len(DataSet)): for j in range(len(DataSet[i])): if is_number(DataSet[i][j]): DataSet[i][j] = float(DataSet[i][j]) return DataSet # convert data-set to float DataSet = convertToFloat(DataSet) def caculateDistance(a: tuple, b: tuple): d = sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2) return d def readLine(m = 0, n = 2, offset = 1): global DataSet lenth = int(DataSet[m][n]) line = [] for i in range(lenth): line.append(DataSet[i + offset]) return line class Person: def __init__(self, infomationLine): self.name, self.score, self.weightOfGift, self.x_position, self.y_position = infomationLine def getPosition(self): return (self.x_position, self.y_position) def getScore(self): global Score Score += self.score class People: info = [] def __init__(self): global DataSet line = readLine(m = 0, n = 3, offset = 1 + int(DataSet[0][2])) for i in line: self.info.append(Person(i)) def visualization(self): x = [] y = [] for i in self.info: x.append(i.x_position) y.append(i.y_position) plt.plot(x, y, 'o') plt.show() class Santa: # (x, y) # TODO because tuple is unchangeable so we can not use it.Maybe we can use a sub-class Dimensionality(2D) to describe 'position' and 'speed'.Just like """ class 2D: x y position:2D position.x += speed*t """ position = (0.0,0.0) speed = (0.0,0.0) weightOfCarrots:int = 0 weightOfGift:int = 0 def __init__(self): global DataSet self.timeLimit = int(DataSet[0][0]) self.deliveryDistanceLimit = int(DataSet[0][1]) self.speedLimitTable = readLine() def getPosition(self): return self.position def getWeight(self): return self.weightOfCarrots + self.weightOfGift def nowSpeedLimit(self): w = self.getWeight() assert w >= 0 if w <= self.speedLimitTable[0][0]: return self.speedLimitTable[0][1] elif w > self.speedLimitTable[0][0] and w <= self.speedLimitTable[1][0]: return self.speedLimitTable[1][1] elif w > self.speedLimitTable[1][0] and w <= self.speedLimitTable[2][0]: return self.speedLimitTable[2][1] elif w > self.speedLimitTable[2][0] and w <= self.speedLimitTable[3][0]: return self.speedLimitTable[3][1] else: return 0 def isAllowedBySpeedLimit(self, s): return s <= self.nowSpeedLimit() def consumeCarrot(self): assert self.weightOfCarrots >= 1 self.weightOfCarrots -= 1 def AccUp(self, s): assert self.isAllowedBySpeedLimit(s) self.speed[1] += s self.consumeCarrot() def AccDown(self, s): assert self.isAllowedBySpeedLimit(s) self.speed[1] -= s self.consumeCarrot() def AccRight(self, s): assert self.isAllowedBySpeedLimit(s) self.speed[0] += s self.consumeCarrot() def AccLeft(self, s): assert self.isAllowedBySpeedLimit(s) self.speed[0] -= s self.consumeCarrot() def LoadCarrots(self, w): self.weightOfCarrots += w def LoadGift(self, w): self.weightOfGift += w def DiliveryGift(self,p: Person): assert caculateDistance(self.getPosition(), p.getPosition()) <= self.deliveryDistanceLimit self.weightOfGift -= p.weightOfGift assert self.weightOfGift >= 0 p.getScore() s = Santa() s.LoadGift(45) pp = People() pp.visualization() s.LoadCarrots(15) s.getWeight() s.isAllowedBySpeedLimit(2) s.deliveryDistanceLimit s.AccDown(2)