Last active
December 24, 2019 15:08
-
-
Save jan-matejka/6559af42fec1b06d245af5fd9b8494f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
class Cost: | |
def __init__(self, w : int = 0, s : int = 0, m : int = 0, hqm : int = 0): | |
self.w = w | |
self.s = s | |
self.m = m | |
self.hqm = hqm | |
if self == ZeroCost(): | |
raise ValueError('All resources are 0') | |
self.w_s = self._slots(self.w, 1000) | |
self.s_s = self._slots(self.s, 1000) | |
self.m_s = self._slots(self.m, 1000) | |
self.hqm_s = self._slots(self.hqm, 100) | |
def __mul__(self, n : int): | |
return Cost(self.w * n, self.s * n, self.m * n, self.hqm * n) | |
def _slots(self, have, cost): | |
return (have // cost) + 1 if have % cost > 0 else 0 | |
def slots(self): | |
return sum(( | |
self._slots(self.w, 1000), | |
self._slots(self.s, 1000), | |
self._slots(self.m, 1000), | |
self._slots(self.hqm, 100), | |
)) | |
def __str__(self): | |
_fmt = "{:5s} = {:2d} {:5d}" | |
return "\n".join(( | |
_fmt.format("wood", self.w_s, self.w), | |
_fmt.format("stone", self.s_s, self.s), | |
_fmt.format("metal", self.m_s, self.m), | |
_fmt.format("hqm", self.hqm_s, self.hqm), | |
)) | |
def __eq__(self, c): | |
return all(( | |
self.w == c.w, | |
self.s == c.s, | |
self.m == c.m, | |
self.hqm == c.hqm | |
)) | |
def __add__(self, c): | |
return Cost( | |
self.w + c.w, | |
self.s + c.s, | |
self.m + c.m, | |
self.hqm + c.hqm, | |
) | |
class ZeroCost(Cost): | |
w = 0 | |
s = 0 | |
m = 0 | |
hqm = 0 | |
def __init__(self): | |
pass | |
def calculate(daily : Cost): | |
tc_fill = daily | |
candidate = daily | |
while tc_fill.slots() < 24: | |
tc_fill = candidate | |
candidate = tc_fill + daily | |
return tc_fill | |
if __name__ == "__main__": | |
print(str(calculate(Cost(205, 419, 582, 30)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment