Solve day 6
This commit is contained in:
28
06/__main__.py
Normal file
28
06/__main__.py
Normal file
@@ -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()
|
21
06/puzzle.py
Normal file
21
06/puzzle.py
Normal file
@@ -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
|
18
06/puzzle_test.py
Normal file
18
06/puzzle_test.py
Normal file
@@ -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
|
Reference in New Issue
Block a user