-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_code.py
115 lines (94 loc) · 3.03 KB
/
hash_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 1 18:14:34 2018
@author: Vincoux & Sam
"""
from sys import argv
class Car:
#x = 0
#y = 0
#travels = []
#available_step = 0
def __init__(self):
self.x = 0
self.y = 0
self.travels = []
self.available_step = 0
@property
def get_pos(self):
return self.x, self.y
def set_pos(self, x_, y_):
self.x = x_
self.y = y_
def how_many_time(self, l):
return distance(self.x, self.y, l[0], l[1]) + distance(l[0], l[1], l[2], l[3])
def attribute(self, index, l, step):
self.travels.append(index)
self.available_step = step + self.how_many_time(l)
self.x = l[2]
self.y = l[3]
#----------------------------- PARSING -----------------------------
data = open(argv[1], 'r').read()
l = data.split('\n')
fl = l[0].split(' ')
row, col, nbv, nbr, bns, steps = [ int(x) for x in fl ]
matrix = []
for i in range(nbr):
matrix.append([int(x) for x in l[i + 1].split(' ')])
#----------------------------- INIT --------------------------------
res = []
for i in range(nbv):
res.append(Car())
#----------------------------- COMPUTE -----------------------------
def is_paid(step, l, car):
return step + car.how_many_time(l) < l[5]
def distance(x1, y1, x2, y2):
return (abs(x1 - x2) + abs(y1 - y2))
def finish(travel_list):
for travel in travel_list:
if not travel:
return False
return True
step = 0
travel_list = [False] * len(matrix)
while (not finish(travel_list)):
for j in range(len(matrix)):
if (matrix[j][4] > step):
#step += 1
continue
if travel_list[j]:
continue
if (finish(travel_list)):
break
mini = 1000000000
mini_index = 0
mini_not_paid = 1000000000
mini_index_not_paid = 0
for i in range(len(res)):
if (res[i].available_step <= step):
if (is_paid(step, matrix[j], res[i])):
if (res[i].how_many_time(matrix[j]) < mini):
mini = res[i].how_many_time(matrix[j])
mini_index = i
else:
if (res[i].how_many_time(matrix[j]) < mini_not_paid):
mini_not_paid = res[i].how_many_time(matrix[j])
mini_index_not_paid = i
if (mini != 1000000000):
res[mini_index].attribute(j, matrix[j], step)
travel_list[j] = True
elif (step > steps/2):
res[mini_index_not_paid].attribute(j, matrix[j], step)
travel_list[j] = True
step += 1
#----------------------------- OUTPUT ------------------------------
result = open(argv[1][:-3] + ".out", 'w')
r = ""
for i in range(nbv):
r += str(len(res[i].travels)) + " "
for j in range(len(res[i].travels)):
r += str(res[i].travels[j]) + (" " if j < len(res[i].travels) - 1 else "")
r += "\n"
result.write(r)
result.close()