-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
288ac9d
commit 0db9282
Showing
4 changed files
with
71 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,59 @@ | ||
import math | ||
|
||
from adventofcode.utils import open_input | ||
|
||
|
||
def main(): | ||
data = open_input('adventofcode/_2023/day6/input.txt') | ||
answer_1 = get_answer(data) | ||
answer_2 = get_answer(data, part_2=True) | ||
|
||
print(answer_1) | ||
print(answer_2) | ||
|
||
return answer_1, answer_2 | ||
|
||
|
||
def get_answer(data, part_2: bool = False): | ||
""" | ||
- paper describes the record distance and how long you get to race it | ||
- starting speed of 0mm per ms | ||
- for each 1ms you hold the button, the boat's speed increases by 1mm per ms | ||
- determine how many times you could beat the record for each race with different combos, multiply all answers | ||
""" | ||
# TODO: This function is SLOW, takes ~3 seconds for part two, this is due to us needlessly checking every | ||
# possibility inbetween the two bookends. A vast performance gain could be made by only checking | ||
# the low and high ends until you find where games are no longer possible and then taking the difference | ||
# between those numbers to get your answer so we don't have to iterate two hundred trillion times | ||
game_results = [] | ||
times = [''.join([i for i in data[0].split()[1:]])] if part_2 else data[0].split()[1:] | ||
records = [''.join([i for i in data[1].split()[1:]])] if part_2 else data[1].split()[1:] | ||
|
||
for i in range(len(times)): | ||
time = int(times[i]) | ||
record = int(records[i]) | ||
game_possibilities = 0 | ||
|
||
for ii in range(time): | ||
if ii == 0: | ||
speed = 0 | ||
time_remaining = time | ||
elif ii + 1 == time: | ||
time_remaining = 0 | ||
else: | ||
speed += 1 | ||
time_remaining -= 1 | ||
|
||
distance = speed * time_remaining | ||
if distance > record: | ||
game_possibilities += 1 | ||
|
||
game_results.append(game_possibilities) | ||
|
||
answer = math.prod(game_results) | ||
|
||
return answer | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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: 40 92 97 90 | ||
Distance: 215 1064 1505 1100 |
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: 7 15 30 | ||
Distance: 9 40 200 |
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,8 @@ | ||
from adventofcode._2023.day6.challenge import main | ||
|
||
|
||
def test_input(): | ||
answer1, answer2 = main() | ||
|
||
assert answer1 == 6209190 | ||
assert answer2 == 28545089 |