Skip to content

Commit

Permalink
2023/06
Browse files Browse the repository at this point in the history
Signed-off-by: Lunik <lunik@tiwabbit.fr>
  • Loading branch information
Lunik committed Dec 9, 2023
1 parent 4ad3733 commit e6aca13
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 4 deletions.
2 changes: 2 additions & 0 deletions adventofcode/solutions/y2023/d06/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 34 90 89 86
Distance: 204 1713 1210 1780
57 changes: 57 additions & 0 deletions adventofcode/solutions/y2023/d06/part1.py
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions adventofcode/solutions/y2023/d06/part2.py
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions adventofcode/tests/y2023/d06/test_y2023_d06.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions docs/report-2023.html

Large diffs are not rendered by default.

0 comments on commit e6aca13

Please sign in to comment.