This commit is contained in:
Tyrel Souza 2023-12-06 09:47:55 -05:00
parent a4ca5f4a4a
commit d921354b2a
No known key found for this signature in database
GPG Key ID: F3614B02ACBE438E

View File

@ -23,35 +23,28 @@ def part1(rows):
# @shared.profile # @shared.profile
def part2(rows): def part2(rows):
# join ints
times = [int("".join([r for r in rows[0].split(":")[1].split(" ") if r]))] times = [int("".join([r for r in rows[0].split(":")[1].split(" ") if r]))]
distances = [int("".join([r for r in rows[1].split(":")[1].split(" ") if r]))] distances = [int("".join([r for r in rows[1].split(":")[1].split(" ") if r]))]
ways = [] ways = []
for game in [Game(*g) for g in zip(times, distances)]: for game in [Game(*g) for g in zip(times, distances)]:
ways.append(calculate(game)) ways.append(calculate(game))
print(reduce(operator.mul, ways, 1)) print(reduce(operator.mul, ways, 1))
def calculate(game): def search(game, _range):
starting_win = None for held in _range:
for held in range(game.duration+1):
starting_win = held
remaining = game.duration - held remaining = game.duration - held
score = held * remaining score = held * remaining
if score > game.highscore: if score > game.highscore:
break return held
ending_win = None def calculate(game):
for held in reversed(range(game.duration+1)): starting_win = search(game, range(game.duration+1))
ending_win = held ending_win = search(game, reversed(range(game.duration+1)))
remaining = game.duration - held
score = held * remaining
if score > game.highscore:
break
print(game, starting_win, ending_win) print(game, starting_win, ending_win)
return ending_win+1-starting_win return ending_win+1-starting_win
def main(): def main():
rows = [row for row in shared.load_rows(6)] rows = [row for row in shared.load_rows(6)]
with shared.elapsed_timer() as elapsed: with shared.elapsed_timer() as elapsed: