Day 5: If You Give a Seed a Fertilizer


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


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Unlocked after 27 mins (current record for time, hard one today)

  • Ategon@programming.devOPM
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    1 year ago

    [JavaScript] Well that was by far the hardest out of all of the days, part 1 was relatively fine but part 2 took me awhile of trying different things

    Ended up solving it by working backwards by trying different location values and seeing if that can become a valid seed. Takes around 3 secs to compute the answer.

    Link to code

    Part 1 Code Block
    // Part 1
    // ======
    
    function part1(input) {
      const split = input.split("\r\n\r\n");
    
      let pastValues = split[0].match(/\d+/g).map((x) => parseInt(x));
      let currentValues = [];
    
      for (const section of split.slice(1)) {
        for (const line of section.split("\r\n")) {
          const values = line.match(/\d+/g)?.map((x) => parseInt(x));
    
          if (!values) {
            continue;
          }
    
          const sourceStart = values[1];
          const destinationStart = values[0];
          const length = values[2];
    
          for (let i = 0; i < pastValues.length; i++) {
            if (
              pastValues[i] >= sourceStart &&
              pastValues[i] < sourceStart + length
            ) {
              currentValues.push(destinationStart + pastValues[i] - sourceStart);
              pastValues.splice(i, 1);
              i--;
            }
          }
        }
    
        for (let i = 0; i < pastValues.length; i++) {
          currentValues.push(pastValues[i]);
        }
    
        pastValues = [...currentValues];
        currentValues = [];
      }
    
      return Math.min(...pastValues);
    }
    
    Part 2 Code Block
    // Part 2
    // ======
    
    function part2(input) {
      const split = input.split("\r\n\r\n");
    
      let seeds = split[0].match(/\d+/g).map((x) => parseInt(x));
      seeds = seeds
        .filter((x, i) => i % 2 == 0)
        .map((x, i) => [x, seeds[i * 2 + 1]]);
    
      const maps = split
        .slice(1)
        .map((x) => {
          const lines = x.split("\r\n");
          return lines
            .map((x) => x.match(/\d+/g)?.map((x) => parseInt(x)))
            .filter((x) => x);
        })
        .reverse();
    
      for (let i = 0; true; i++) {
        let curValue = i;
    
        for (const map of maps) {
          for (const line of map) {
            const sourceStart = line[1];
            const destinationStart = line[0];
            const length = line[2];
    
            if (
              curValue >= destinationStart &&
              curValue < destinationStart + length
            ) {
              curValue = sourceStart + curValue - destinationStart;
              break;
            }
          }
        }
    
        for (const [seedRangeStart, seedRangeLength] of seeds) {
          if (
            curValue >= seedRangeStart &&
            curValue < seedRangeStart + seedRangeLength
          ) {
            return i;
          }
        }
      }
    }
    
    • cacheson@kbin.social
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      Ended up solving it by working backwards by trying different location values and seeing if that can become a valid seed.

      Huh, that’s clever.

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

        Turns out I got really lucky and my location value is much lower than most peoples which is why it can be solved relatively quickly

    • Leo Uino@lemmy.sdf.org
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      Torn between doing the problem backwards and implementing a more general case – glad to know both approaches work out in the end!