Day06 speed improvements by simple math
This commit is contained in:
parent
fa268e411e
commit
61cf84be97
1 changed files with 6 additions and 13 deletions
19
aoc/day06.py
19
aoc/day06.py
|
@ -1,5 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
from functools import reduce
|
||||
from math import ceil, floor, sqrt
|
||||
|
||||
import re
|
||||
|
||||
|
@ -13,9 +14,10 @@ class Race:
|
|||
|
||||
|
||||
class Day06(AocSameImplementation[list[Race]], Aoc2[list[Race], list[Race]]):
|
||||
def simulate(self, timeaccel: int, timeoverall: int) -> int:
|
||||
racetime = timeoverall - timeaccel
|
||||
return racetime * timeaccel
|
||||
def is_zero(self, to: int, d: int) -> tuple[int, int]:
|
||||
return floor(to / 2 - sqrt((-to / 2) ** 2 - d) + 1), ceil(
|
||||
to / 2 + sqrt((-to / 2) ** 2 - d) - 1
|
||||
)
|
||||
|
||||
def parseinput2(self, inpt: str) -> list[Race]:
|
||||
lines = inpt.splitlines()
|
||||
|
@ -34,16 +36,7 @@ class Day06(AocSameImplementation[list[Race]], Aoc2[list[Race], list[Race]]):
|
|||
def part(self, inpt: list[Race]) -> int:
|
||||
return reduce(
|
||||
lambda x, y: x * y,
|
||||
[
|
||||
len(
|
||||
[
|
||||
poss
|
||||
for poss in range(race.time)
|
||||
if self.simulate(poss, race.time) > race.distance
|
||||
]
|
||||
)
|
||||
for race in inpt
|
||||
],
|
||||
(e - s + 1 for s, e in (self.is_zero(r.time, r.distance) for r in inpt)),
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue