Skip to content

Commit

Permalink
2023 day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacerulerwill committed Jan 10, 2025
1 parent 7b7536d commit 667f69d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
23 changes: 23 additions & 0 deletions puzzles/Y2023/D6/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import re
from typing import Any

digits = re.compile("([0-9]+)")


def puzzle(input: str) -> Any:
lines = [line.strip() for line in input.splitlines()]
times = [int(num) for num in re.findall(digits, lines[0])]
record_distances = [int(num) for num in re.findall(digits, lines[1])]
num_races = len(times)

total = 1
for race_index in range(num_races):
race_time = times[race_index]
race_record = record_distances[race_index]
winning_combinations = 0
for i in range(race_time + 1):
distance_travelled = i * (race_time - i)
if distance_travelled > race_record:
winning_combinations += 1
total *= winning_combinations
return total
27 changes: 27 additions & 0 deletions puzzles/Y2023/D6/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import re
from typing import Any

digits = re.compile("([0-9]+)")


def puzzle(input: str) -> Any:
lines = [line.strip() for line in input.splitlines()]
time = int("".join(num for num in re.findall(digits, lines[0])))
record_distance = int("".join(num for num in re.findall(digits, lines[1])))

# iterate from start to find first value of i that wins a race
first = 0
for i in range(time + 1):
distance_travelled = i * (time - i)
if distance_travelled > record_distance:
first = i
break

# iterate from the end to find the last value of i that wins a race
last = 0
for i in range(time + 1, -1, -1):
distance_travelled = i * (time - i)
if distance_travelled > record_distance:
last = i
break
return last - first + 1

0 comments on commit 667f69d

Please sign in to comment.