polyhash2022/hashcode2022_V_group.py

445 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""HashCode2022_BUG.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PiwWB2bonQgoAGp6woFuJhFi2qq0_aj7
"""
from google.colab import drive
drive.mount('/content/drive')
from math import sqrt
import matplotlib.pyplot as plt
import numpy as np
import os
import math
File_Directory = 'drive/MyDrive/Ecole/PolyHash2022/DataSet/'
File_Name = 'f_festive_flyover.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: list, 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:
priority:float
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
def caculatePriority(self):
return self.score / (self.weightOfGift * caculateDistance([0,0], (self.x_position, self.y_position)))
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:
tableauDeDistribution = []
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
#[[2000.0, 20.0], [3000.0, 15.0], [5000.0, 10.0], [6000.0, 9.0], [7000.0, 8.0]]
def nowSpeedLimit(self):
w = self.getWeight()
assert w >= 0
for i in range(len(self.speedLimitTable)):
if w < self.speedLimitTable[i][0]:
return self.speedLimitTable[i][1]
return 0
# DONE 此速度限制speedLimit为限制表不是当前限制我们下一步要定义nowspeedLimit
def isAllowedBySpeedLimit(self, s):
return s <= self.nowSpeedLimit()
def consumeCarrot(self):
assert self.weightOfCarrots >= 1
self.weightOfCarrots -= 1
def AccUp(self, s):
if self.timeLimit>0:
assert self.isAllowedBySpeedLimit(s)
self.speed[1] += s
self.consumeCarrot()
print("AccUp:",s)
else:
pass
def AccDown(self, s):
if self.timeLimit>0:
assert self.isAllowedBySpeedLimit(s)
self.speed[1] -= s
self.consumeCarrot()
print("AccLeft:",s)
else:
pass
def AccRight(self, s):
if self.timeLimit>0:
assert self.isAllowedBySpeedLimit(s)
self.speed[0] += s
self.consumeCarrot()
print("AccRight:",s)
else:
pass
def AccLeft(self, s):
if self.timeLimit>0:
assert self.isAllowedBySpeedLimit(s)
self.speed[0] -= s
self.consumeCarrot()
print("AccLeft:",s)
else:
pass
def LoadCarrots(self, w):
self.weightOfCarrots += w
def LoadGift(self, w):
self.weightOfGift += w
def DiliveryGift(self,p: Person):
if self.timeLimit >=0 and caculateDistance(self.getPosition(), p.getPosition()) <= self.deliveryDistanceLimit:
assert caculateDistance(self.getPosition(), p.getPosition()) <= self.deliveryDistanceLimit
self.weightOfGift -= p.weightOfGift
assert self.weightOfGift >= 0
p.getScore()
print("Delivert gift to",p.name)
else:
pass
def Float(self, t:int = 1):
if (self.timeLimit - t) >=0:
self.position[0] += self.speed[0] * t
self.position[1] += self.speed[1] * t
self.timeLimit -= t
print("Float:",t)
else:
pass
def getSituation(self):
print("Now position is:",self.position)
print("Now speed is:",self.speed)
print("Now timeLimit is:",self.timeLimit)
print("Now weightOfGift is:",self.weightOfGift)
print("Now weightOfCarrots is:",self.weightOfCarrots)
def generateTableauDeDistribution(self, pp: People):
self.tableauDeDistribution = sorted(pp.info, key=lambda x: x.caculatePriority(), reverse=True)
return self.tableauDeDistribution
def kidoneWay(self,x:int,p:Person):
v = self.nowSpeedLimit()
if isinstance(x/v,int) and x > 0 :
t = x/v
self.AccUp(v)
self.Float(t)
self.AccDown(v)
self.Float(1)
self.DiliveryGift(p)
t = x/v
self.AccDown(v)
self.Float(t)
self.AccUp(v)
self.Float(1)
elif isinstance(x/v,int) and x < 0 :
t = abs(x)/v
self.AccDown(v)
self.Float(t)
self.AccUp(v)
self.Float(1)
self.DiliveryGift(p)
t = abs(x)/v
self.AccUp(v)
self.Float(t)
self.AccDown(v)
self.Float(1)
elif x < v and x > 0 :
v = x
self.AccUp(v)
self.Float(1)
self.AccDown(v)
self.Float(1)
self.DiliveryGift(p)
v = x
self.AccDown(v)
self.Float()
self.AccUp(v)
self.Float(1)
elif abs(x) < v and x < 0 :
v = abs(x)
self.AccDown(v)
self.Float(1)
self.AccUp(v)
self.Float(1)
self.DiliveryGift(p)
v = x
self.AccUp(v)
self.Float(1)
self.AccDown(v)
self.Float(1)
elif x > v and x > 0 :
t = x//v
v1 = v - (x - t*v)
self.AccUp(v)
self.Float(t)
self.AccDown(v1)
self.Float()
self.AccDown(v - v1)
self.Float(1)
self.DiliveryGift(p)
t = x//v
v1 = v - (x - t*v)
self.AccDown(v)
self.Float(t)
self.AccUp(v1)
self.Float(1)
self.AccUp(v - v1)
self.Float(1)
elif abs(x) > v and x < 0 :
t = abs(x)//v
v1 = v - (abs(x) - t*v)
self.AccDown(v)
self.Float(t)
self.AccUp(v1)
self.Float(1)
self.AccUp(v - v1)
self.Float(1)
self.DiliveryGift(p)
t = abs(x)//v
v1 = v - (abs(x) - t*v)
self.AccUp(v)
self.Float(t)
self.AccDown(v1)
self.Float(1)
self.AccDown(v - v1)
self.Float(1)
def oneWay(self, p:Person):
if self.timeLimit >0:
self.LoadCarrots(12)
print("LoarCarrots:12")
self.LoadGift(p.weightOfGift)
print("LoarGift:",p.weightOfGift)
v = self.nowSpeedLimit()
if isinstance(p.x_position/v,int) and p.x_position > 0 :
t = p.x_position/v
self.AccRight(v)
self.Float(t)
self.AccLeft(v)
self.Float(1)
self.kidoneWay(p.y_position,p)
t = p.x_position/v
self.AccLeft(v)
self.Float(t)
self.AccRight(v)
self.Float()
elif isinstance(p.x_position/v,int) and p.x_position < 0:
t = abs(p.x_position)/v
self.AccLeft(v)
self.Float(t)
self.AccRight(v)
self.Float(1)
self.kidoneWay(p.y_position,p)
t = abs(p.x_position)/v
self.AccRight(v)
self.Float(t)
self.AccLeft(v)
self.Float(1)
elif p.x_position < v and p.x_position > 0 :
v = p.x_position
self.AccRight(v)
self.Float(1)
self.AccLeft(v)
self.Float(1)
self.kidoneWay(p.y_position,p)
v = p.x_position
self.AccLeft(v)
self.Float(1)
self.AccRight(v)
self.Float(1)
elif abs(p.x_position) < v and p.x_position < 0 :
v = abs(p.x_position)
self.AccLeft(v)
self.Float(1)
self.AccRight(v)
self.Float(1)
self.kidoneWay(p.y_position,p)
v = p.x_position
self.AccRight(v)
self.Float(1)
self.AccLeft(v)
self.Float(1)
elif p.x_position > v and p.x_position > 0 :
t = p.x_position//v
v1 = v - (p.x_position - t*v)
self.AccRight(v)
self.Float(t)
self.AccLeft(v1)
self.Float(1)
self.AccLeft(v - v1)
self.Float(1)
self.kidoneWay(p.y_position,p)
t = p.x_position//v
v1 = v - (p.x_position - t*v)
self.AccLeft(v)
self.Float(t)
self.AccRight(v1)
self.Float(1)
self.AccRight(v - v1)
self.Float(1)
elif abs(p.x_position) > v and p.x_position < 0 :
t = abs(p.x_position)//v
v1 = v - (abs(p.x_position) - t*v)
self.AccLeft(v)
self.Float(t)
self.AccRight(v1)
self.Float(1)
self.AccRight(v - v1)
self.Float(1)
self.kidoneWay(p.y_position,p)
t = abs(p.x_position)//v
v1 = v - (abs(p.x_position) - t*v)
self.AccRight(v)
self.Float(t)
self.AccLeft(v1)
self.Float(1)
self.AccLeft(v - v1)
self.Float(1)
elif p.x_position == 0 :
self.kidoneWay(p.y_position,p)
print("Get score:",Score)
s = Santa()
s.speedLimitTable
pp = People()
pp.info[0]
pp.visualization()
s.generateTableauDeDistribution(pp)
for i in s.generateTableauDeDistribution(pp):
s.oneWay(i)
s.getSituation()
Score