diff --git a/2023/python/day06.py b/2023/python/day06.py index 72a5cde..61600d4 100644 --- a/2023/python/day06.py +++ b/2023/python/day06.py @@ -23,35 +23,28 @@ def part1(rows): # @shared.profile def part2(rows): + # join ints 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]))] + ways = [] for game in [Game(*g) for g in zip(times, distances)]: ways.append(calculate(game)) - print(reduce(operator.mul, ways, 1)) -def calculate(game): - starting_win = None - for held in range(game.duration+1): - starting_win = held +def search(game, _range): + for held in _range: remaining = game.duration - held score = held * remaining if score > game.highscore: - break + return held - ending_win = None - for held in reversed(range(game.duration+1)): - ending_win = held - remaining = game.duration - held - score = held * remaining - if score > game.highscore: - break +def calculate(game): + starting_win = search(game, range(game.duration+1)) + ending_win = search(game, reversed(range(game.duration+1))) print(game, starting_win, ending_win) return ending_win+1-starting_win - - def main(): rows = [row for row in shared.load_rows(6)] with shared.elapsed_timer() as elapsed: