Logo

dev-resources.site

for different kinds of informations.

Red-Nosed Reports

Published at
12/3/2024
Categories
adventofcode
algorithms
javascript
programming
Author
rmion
Author
5 person written this
rmion
open
Red-Nosed Reports

Advent of Code 2024 Day 2

Part 1

To just solve, or to solve optimally? That is the question

To just solve:

  • Compare the list in original order to two copies, each sorted, but in reverse orders. If one is a match, then success for criteria one
  • Iterate through every item in the list, except the first. Track the difference of each item and previous item. If any differences are 0 or greater than 3, then this test fails.

Performance-wise, that means:
For criteria 1:

  • Two list copies
  • Sorting each copy
  • Comparing original list twice For criteria 2:
  • Check every number in each list

To solve optimally:

  • For criteria 2, I'll leverage a while loop to check subsequent numbers for valid differences. That way, as soon as a disqualifying difference occurs, the rest of the numbers won't get processed
  • For criteria 1, I'll track the differences, then do one check for whether all are less than or greater than zero

I think this will work. Only one way to find out.

Writing an optimized algorithm

Here's my code for identifying criteria 2 (difference of 1, 2, or 3):

let differFlag = true;
let i = 1;
while (differFlag && i < list.length) {
  let amount = Math.abs(list[i] - list[i - 1]);
  if (![1, 2, 3].includes(amount)) {
    differFlag = false;
  }
  i++;
}
Enter fullscreen mode Exit fullscreen mode
  • I only 'walk' the list until an invalid difference is caught, if any
  • As soon as one is caught, the while loop exits
  • After the while loop, I can check differFlag for the results

Here's my code for identifying criteria 1 (all differences increase or decrease):

let differFlag = true;
let i = 1;
let differences = [];
while (differFlag && i < list.length) {
  let amount = list[i] - list[i - 1];
  differences.push(amount);
  if (![1, 2, 3].includes(Math.abs(amount))) {
    differFlag = false;
  }
  i++;
}
Enter fullscreen mode Exit fullscreen mode
  • I create a list of each difference
  • I moved the absolute value calculation to the conditional because I actually want to capture the sign of the difference
  • After the while loop, I can check differences to see whether every value is positive or negative

Here's the final condition that captures a safe report:

if (
     differFlag &&
    (differences.every((el) => el > 0) || 
     differences.every((el) => el < 0))
    ) {
      safeCount++;
    }
Enter fullscreen mode Exit fullscreen mode

Altogether, my algorithm generates the correct answer for the example input.

Will it do the same for my puzzle input??

Yessssirrrreeee!!

Sweet!

Part 2

Welllll...shoot.

This certainly complicates things a bit.

I'd like to avoid an algorithm that checks every possible permutation of a report. That would require generating millions of reports.

The first bit of good news is:

  • All safe reports can still be counted as safe

For my puzzle input, that's about 200 that don't require me to check permutations.

Still, 800/1000 is still a lot of lists to fully explore permutations.

I honestly don't see a way to avoid running my algorithm on each permutation of unsafe reports.

Bummer.

Time to add a loop to iterate through each number in unsafe reports - the number to remove and then check the mutated list for a passing grade.

Adding a permutation-checking loop

I ended up duplicating my while loop with the added lines to duplicate and remove one number from each subsequent test report.

It's a lot more code.

But, it works! I generate the correct answer for the puzzle input!

Question is:

  • Will it run...and generate the correct answer for my puzzle input?

Let's run it and see...

Hmmm, it runs, but I get an answer that it only slightly greater than my Part 1 answer. That seems wrong.

Doesn't hurt to submit it, right????

It is correct!

Holy smokes!

That's incredible!

And really fun to solve!

Four gold stars going into Day 3.

Bring on more wonderful puzzles!

adventofcode Article's
30 articles in total
Favicon
Climbing a depth-first search hill, Advent of Code 2024, day 10
Favicon
Hoof It
Favicon
How to parse computer code, Advent of Code 2024 day 3
Favicon
Disk Fragmenter
Favicon
How to repair a bridge, Advent of Code 2024 Day 7
Favicon
Resonant Collinearity
Favicon
Advent of Code 2024 - Day 23: LAN Party
Favicon
How to trace steps in a map, Advent of Code 2024 day 6
Favicon
Advent of Code 2024 - Day 21: Keypad Conundrum
Favicon
Advent of Code 2024 - Day 18: Ram Run
Favicon
Advent of Code 2024 - Day 17: Chronospatial Computer
Favicon
Guard Gallivant
Favicon
Advent of Code 2024 Day 5 in Golang: Ordering Pages
Favicon
Advent of Code '24 - Day9: Disk Fragmenter (Python)
Favicon
Advent of Code #8 (in Gleam)
Favicon
Advent of Code #7 (in Gleam)
Favicon
Advent of Code #6 (in Gleam)
Favicon
Advent of Code #5 (in Gleam)
Favicon
AoC 24 - Day 4: Ceres Search
Favicon
Wednesday Links – Edition 2024-12-04
Favicon
Mull It Over
Favicon
Bridge Repair
Favicon
Advent of Code #3 (in Gleam)
Favicon
Day 3: Mull It Over | Advent of Code 2024 | Swift | δΈ­ζ–‡
Favicon
Red-Nosed Reports
Favicon
Day 1: Historian Hysteria | Advent of Code 2024 | Swift | δΈ­ζ–‡
Favicon
Advent of Code 2024 - Day 19: Linen Layout
Favicon
Advent of Code - It's More Than a Competition
Favicon
Advent of Code '24 - Day 10: Hoof It
Favicon
Advent of Code 2024 Retro: What could you do if you didn't care whether you failed?

Featured ones: