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

  • Ategon@programming.devOPM
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    11 months ago

    [JavaScript] Relatively easy one today

    Paste

    Part 1
    function part1(input) {
      const split = input.split("\n");
      const times = split[0].match(/\d+/g).map((x) => parseInt(x));
      const distances = split[1].match(/\d+/g).map((x) => parseInt(x));
    
      let sum = 0;
    
      for (let i = 0; i < times.length; i++) {
        const time = times[i];
        const recordDistance = distances[i];
    
        let count = 0;
    
        for (let j = 0; j < time; j++) {
          const timePressed = j;
          const remainingTime = time - j;
    
          const travelledDistance = timePressed * remainingTime;
    
          if (travelledDistance > recordDistance) {
            count++;
          }
        }
    
        if (sum == 0) {
          sum = count;
        } else {
          sum = sum * count;
        }
      }
    
      return sum;
    }
    
    Part 2
    function part2(input) {
      const split = input.split("\n");
      const time = parseInt(split[0].split(":")[1].replace(/\s/g, ""));
      const recordDistance = parseInt(split[1].split(":")[1].replace(/\s/g, ""));
    
      let count = 0;
    
      for (let j = 0; j < time; j++) {
        const timePressed = j;
        const remainingTime = time - j;
    
        const travelledDistance = timePressed * remainingTime;
    
        if (travelledDistance > recordDistance) {
          count++;
        }
      }
    
      return count;
    }
    

    Was a bit late with posting the solution thread and solving this since I ended up napping until 2am, if anyone notices theres no solution thread and its after the leaderboard has been filled (can check from the stats page if 100 people are done) feel free to start one up (I just copy paste the text in each of them)