Skip to content

Commit

Permalink
feat: 2023 day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Dec 6, 2023
1 parent 288ac9d commit 0db9282
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
59 changes: 59 additions & 0 deletions adventofcode/_2023/day6/challenge.py
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()
2 changes: 2 additions & 0 deletions adventofcode/_2023/day6/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 40 92 97 90
Distance: 215 1064 1505 1100
2 changes: 2 additions & 0 deletions adventofcode/_2023/day6/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200
8 changes: 8 additions & 0 deletions test/unit/_2023/test_day6.py
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

0 comments on commit 0db9282

Please sign in to comment.