2015: d14: ex1: add solution
This commit is contained in:
parent
c0a1916466
commit
b634beec24
1 changed files with 39 additions and 0 deletions
39
2015/d14/ex1/ex1.py
Executable file
39
2015/d14/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class RunPerformance(NamedTuple):
|
||||
speed: int
|
||||
time: int
|
||||
rest: int
|
||||
|
||||
def run(self, t: int) -> int:
|
||||
cycle_length = self.time + self.rest
|
||||
cycles = t // cycle_length
|
||||
left_over = t % cycle_length
|
||||
run_time = cycles * self.time + min(self.time, left_over)
|
||||
return self.speed * run_time
|
||||
|
||||
|
||||
def solve(input: str) -> int:
|
||||
def parse_line(input: str) -> tuple[str, RunPerformance]:
|
||||
split_input = input.split()
|
||||
speed, time, rest = map(int, (split_input[3], split_input[6], split_input[-2]))
|
||||
return split_input[0], RunPerformance(speed, time, rest)
|
||||
|
||||
def parse(input: str) -> dict[str, RunPerformance]:
|
||||
return {name: perf for name, perf in map(parse_line, input.splitlines())}
|
||||
|
||||
reindeers = parse(input)
|
||||
return max(perf.run(2503) for perf in reindeers.values())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue