-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
113 lines (99 loc) · 3.24 KB
/
Copy pathsolver.py
File metadata and controls
113 lines (99 loc) · 3.24 KB
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
import re
import subprocess
import math
import time
# what percentage of people are we letting to die
death_limit = 3 # adjust according to the game evaluation of performance
MAXYEAR = 10
def main():
process = subprocess.Popen(
['python3', 'hammurabi.py'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1
)
for year in range(1, MAXYEAR+1):
print("Yea: " + str(year))
while True:
output = process.stdout.readline()
match = re.search(r"POPULATION IS NOW (\d+)", output)
if match:
population = int(match.group(1))
print("Pop: " + str(population))
break
else:
continue
while True:
output = process.stdout.readline()
match = re.search(r"THE CITY NOW OWNS (\d+)", output)
if match:
acres = int(match.group(1))
print("Acr: " + str(acres))
break
else:
continue
while True:
output = process.stdout.readline()
match = re.search(r"YOU NOW HAVE (\d+)", output)
if match:
stored = int(match.group(1))
print("Sto: " + str(stored))
break
else:
continue
while True:
output = process.stdout.readline()
match = re.search(r"LAND IS TRADING AT (\d+)", output)
if match:
price = int(match.group(1))
print("Pri: " + str(price))
break
else:
continue
if year == MAXYEAR:
# kill as many people in the last year as you can to have the highest possible performance
survivors = math.ceil(population * ((100 - death_limit * MAXYEAR) / 100))
else:
survivors = population
x = 0
x1 = (stored - 20 * survivors - 5 * population) / price
x2 = (stored - 20 * survivors - (acres / 2)) / (price + 0.5)
if x1 > 10 * population - acres:
x = math.floor(x1)
else:
x = math.floor(x2)
acres += x
stored -= price * x
if x > 0:
process.stdin.write(f"{x}\n")
process.stdin.flush()
process.stdin.write(f"0\n")
process.stdin.flush()
else:
process.stdin.write(f"0\n")
process.stdin.flush()
process.stdin.write(f"{abs(x)}\n")
process.stdin.flush()
food = 20 * survivors
process.stdin.write(f"{food}\n")
process.stdin.flush()
plant = min(acres, min(2 * stored, 10 * population))
process.stdin.write(f"{plant}\n")
process.stdin.flush()
print()
while True:
output = process.stdout.readline()
match = re.search(r"HAMURABI: I BEG TO REPORT", output)
if match:
while len(output):
print(output, end="")
output = process.stdout.readline()
break
process.stdin.close()
process.stdout.close()
process.stderr.close()
process.wait()
if __name__ == "__main__":
main()