this post was submitted on 06 Dec 2023
22 points (95.8% liked)

Advent Of Code

158 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 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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 2 points 11 months ago* (last edited 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)