-
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.
- Loading branch information
1 parent
7b7536d
commit 667f69d
Showing
2 changed files
with
50 additions
and
0 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,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 |
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,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 |