From 720def8fa945a9033adb71d6cf4d13d7f99dd1dd Mon Sep 17 00:00:00 2001 From: Nettika Date: Sun, 10 Dec 2023 18:44:26 -0800 Subject: [PATCH] Solve day 6 --- 06/__main__.py | 28 ++++++++++++++++++++++++++++ 06/puzzle.py | 21 +++++++++++++++++++++ 06/puzzle_test.py | 18 ++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 06/__main__.py create mode 100644 06/puzzle.py create mode 100644 06/puzzle_test.py diff --git a/06/__main__.py b/06/__main__.py new file mode 100644 index 0000000..406b6d1 --- /dev/null +++ b/06/__main__.py @@ -0,0 +1,28 @@ +from pathlib import Path + +from puzzle import Race, solve + + +def main(): + print("Permutations of Winning Strategies:") + print( + "Part 1 -", + solve( + [ + Race(61, 430), + Race(67, 1036), + Race(75, 1307), + Race(71, 1150), + ] + ), + ) + print( + "Part 2 -", + solve( + [Race(61677571, 430103613071150)], + ), + ) + + +if __name__ == "__main__": + main() diff --git a/06/puzzle.py b/06/puzzle.py new file mode 100644 index 0000000..8f4b67d --- /dev/null +++ b/06/puzzle.py @@ -0,0 +1,21 @@ +from typing import NamedTuple + + +class Race(NamedTuple): + time: int + distance: int + + +def solve(input: list[Race]): + permutations_total = 1 + + for race in input: + strategies_total = 0 + for hold_time in range(race.time): + race_time = race.time - hold_time + realized_distance = race_time * hold_time + if realized_distance > race.distance: + strategies_total += 1 + permutations_total = permutations_total * strategies_total + + return permutations_total diff --git a/06/puzzle_test.py b/06/puzzle_test.py new file mode 100644 index 0000000..1eca58f --- /dev/null +++ b/06/puzzle_test.py @@ -0,0 +1,18 @@ +from puzzle import Race, solve + + +def test_solve_pt_1(): + assert ( + solve( + [ + Race(7, 9), + Race(15, 40), + Race(30, 200), + ] + ) + == 288 + ) + + +def test_solve_pt_2(): + assert solve([Race(71530, 940200)]) == 71503