this post was submitted on 05 Dec 2023
34 points (100.0% liked)

Advent Of Code

768 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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)

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 5 points 11 months ago* (last edited 11 months ago) (2 children)

[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;
      }
    }
  }
}

[–] [email protected] 1 points 11 months ago (1 children)

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.

[–] [email protected] 1 points 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

[–] [email protected] 1 points 11 months ago

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