From a4ca5f4a4ac0d06526da2ff35b80fc3777368bc4 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 6 Dec 2023 09:44:07 -0500 Subject: [PATCH] day6 part 1 and 2 --- 2023/full/day06.txt | 2 ++ 2023/python/day06.py | 68 ++++++++++++++++++++++++++++++++++++++++++ 2023/samples/day06.txt | 2 ++ 3 files changed, 72 insertions(+) create mode 100644 2023/full/day06.txt create mode 100644 2023/python/day06.py create mode 100644 2023/samples/day06.txt diff --git a/2023/full/day06.txt b/2023/full/day06.txt new file mode 100644 index 0000000..078c0bb --- /dev/null +++ b/2023/full/day06.txt @@ -0,0 +1,2 @@ +Time: 56 71 79 99 +Distance: 334 1135 1350 2430 diff --git a/2023/python/day06.py b/2023/python/day06.py new file mode 100644 index 0000000..72a5cde --- /dev/null +++ b/2023/python/day06.py @@ -0,0 +1,68 @@ +import matrix +import shared +from dataclasses import dataclass +from functools import reduce # Valid in Python 2.6+, required in Python 3 +import operator + + + +@dataclass +class Game: + duration: int + highscore: int + +# @shared.profile +def part1(rows): + times = [int(r) for r in rows[0].split(":")[1].split(" ") if r] + distances = [int(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)) + + +# @shared.profile +def part2(rows): + 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 + remaining = game.duration - held + score = held * remaining + if score > game.highscore: + break + + 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 + 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: + part1(rows) + print("🕒", elapsed()) + + rows = [row for row in shared.load_rows(6,True)] + with shared.elapsed_timer() as elapsed: + part2(rows) + print("🕒", elapsed()) + + +if __name__ == "__main__": + main() diff --git a/2023/samples/day06.txt b/2023/samples/day06.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/2023/samples/day06.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200