-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lunik <lunik@tiwabbit.fr>
- Loading branch information
Showing
5 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 34 90 89 86 | ||
Distance: 204 1713 1210 1780 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.