From e6aca13fdc908cfada61ebfba93ef240ae42cbc8 Mon Sep 17 00:00:00 2001 From: Lunik Date: Sat, 9 Dec 2023 19:12:36 +0100 Subject: [PATCH] 2023/06 Signed-off-by: Lunik --- adventofcode/solutions/y2023/d06/input.txt | 2 + adventofcode/solutions/y2023/d06/part1.py | 57 +++++++++++++++++++ adventofcode/solutions/y2023/d06/part2.py | 36 ++++++++++++ .../tests/y2023/d06/test_y2023_d06.py | 12 ++++ docs/report-2023.html | 8 +-- 5 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 adventofcode/solutions/y2023/d06/input.txt create mode 100644 adventofcode/solutions/y2023/d06/part1.py create mode 100644 adventofcode/solutions/y2023/d06/part2.py create mode 100644 adventofcode/tests/y2023/d06/test_y2023_d06.py diff --git a/adventofcode/solutions/y2023/d06/input.txt b/adventofcode/solutions/y2023/d06/input.txt new file mode 100644 index 0000000..da04254 --- /dev/null +++ b/adventofcode/solutions/y2023/d06/input.txt @@ -0,0 +1,2 @@ +Time: 34 90 89 86 +Distance: 204 1713 1210 1780 \ No newline at end of file diff --git a/adventofcode/solutions/y2023/d06/part1.py b/adventofcode/solutions/y2023/d06/part1.py new file mode 100644 index 0000000..ef3516d --- /dev/null +++ b/adventofcode/solutions/y2023/d06/part1.py @@ -0,0 +1,57 @@ +import os +import cProfile +import pstats +import re + + +def parse(file): + races = [] + + times = re.findall(r"\d+", file.readline()) + distance = re.findall(r"\d+", file.readline()) + + for i in range(len(times)): + races.append({"time": int(times[i]), "max_distance": int(distance[i])}) + + return races + + +def solve(races): + solution = 1 + + for race in races: + for hold_time in range(1, race["time"]): + left_time = race["time"] - hold_time + distance_made = left_time * hold_time + if distance_made >= race["max_distance"]: + first_win = hold_time + break + + for hold_time in range(race["time"] - 1, 0, -1): + left_time = race["time"] - hold_time + distance_made = left_time * hold_time + if distance_made >= race["max_distance"]: + last_win = hold_time + break + + solution *= (last_win - first_win) + 1 + + return solution + + +def main(): + with open( + os.path.join(os.path.dirname(__file__), "input.txt"), "r", encoding="UTF-8" + ) as file: + data = parse(file) + + return solve(data) + + +if __name__ == "__main__": + with cProfile.Profile() as pr: + print(main()) + + stats = pstats.Stats(pr) + stats.sort_stats(pstats.SortKey.TIME) + stats.print_stats() diff --git a/adventofcode/solutions/y2023/d06/part2.py b/adventofcode/solutions/y2023/d06/part2.py new file mode 100644 index 0000000..be1df91 --- /dev/null +++ b/adventofcode/solutions/y2023/d06/part2.py @@ -0,0 +1,36 @@ +import os +import cProfile +import pstats +import re + +from adventofcode.solutions.y2023.d06.part1 import solve + + +def parse(file): + races = [] + + times = [file.readline().split(":")[1].replace(" ", "")] + distance = [file.readline().split(":")[1].replace(" ", "")] + + for i in range(len(times)): + races.append({"time": int(times[i]), "max_distance": int(distance[i])}) + + return races + + +def main(): + with open( + os.path.join(os.path.dirname(__file__), "input.txt"), "r", encoding="UTF-8" + ) as file: + data = parse(file) + + return solve(data) + + +if __name__ == "__main__": + with cProfile.Profile() as pr: + print(main()) + + stats = pstats.Stats(pr) + stats.sort_stats(pstats.SortKey.TIME) + stats.print_stats() diff --git a/adventofcode/tests/y2023/d06/test_y2023_d06.py b/adventofcode/tests/y2023/d06/test_y2023_d06.py new file mode 100644 index 0000000..a849041 --- /dev/null +++ b/adventofcode/tests/y2023/d06/test_y2023_d06.py @@ -0,0 +1,12 @@ +from adventofcode.tests.utils.bench import calculate_duration + +from adventofcode.solutions.y2023.d06.part1 import main as mainPart1 +from adventofcode.solutions.y2023.d06.part2 import main as mainPart2 + + +def test_part1(): + assert calculate_duration(mainPart1) == 633080 + + +def test_part2(): + assert calculate_duration(mainPart2) == 20048741 diff --git a/docs/report-2023.html b/docs/report-2023.html index 68e47d3..ed01f99 100644 --- a/docs/report-2023.html +++ b/docs/report-2023.html @@ -7,7 +7,7 @@

report-2023.html

-

Report generated on 09-Dec-2023 at 18:41:51 by pytest-html +

Report generated on 09-Dec-2023 at 19:12:22 by pytest-html v4.1.1

Environment

@@ -61,7 +61,7 @@

Environment

Summary

-

10 tests took 262 ms.

+

12 tests took 00:00:02.

(Un)check the boxes to filter the results.