Day 6: Wait for It


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

  • bamboo@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    Not sure if it’s the most optimal, but I figured it’s probably quicker to calculate the first point when you start winning, and then reverse it to get the last point when you’ll last win. Subtracting the two to get the total number of ways to win.

    Takes about 3 seconds to run on the real input

    Python Solution
    class Race:
        def __init__(self, time, distance):
            self.time = time
            self.distance = distance
    
        def get_win(self, start, stop, step):
            for i in range(start, stop, step):
                if (self.time - i) * i > self.distance:
                    return i
    
        def get_winners(self):
            return (
                self.get_win(0, self.time, 1),
                self.get_win(self.time, 0, -1),
            )
    
    race = Race(71530, 940200)
    winners = race.get_winners()
    print(winners[1] - winners[0] + 1)