Arpit J | Notes

Advent of Code 2024

I have decided to challenge my problem solving by following through on Advent of Code this year.

“Advent of Code is an Advent calendar of small programming puzzles for a variety of skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.”

I’m solving the problems in Java since I use it at work and feel most comfortable with it. The solutions are up here in this repository.

⚠️ Caution: This Java code works… mostly. Optimization? wHAT’S THAT?
(If you’re a recruiter, rest assured, I know how to optimize. This is just for the LOLs :P)

Day 1

Day 1 was majorly figuring out how to set up the project since I had never solved logical problems like the ones presented in AOC. I got to the first question of AOC 2024 right after.

The question

The logic was clearly stated in the question. Starting with the smallest element of two lists, we had to find the distance between them, up till the distance between the largest elements and add them up.

I took some more time figuring out how to read the input text file and settled with a:

lines = Files.readAllLines(Paths.get("src/resources/input/day1.txt"));

The pseudocode I drafted for solving this problem looked like this -

  1. Split both input columns into separate lists
  2. Initialise a variable distance = 0
  3. For loop till list size
         - Subtract smallest element in first list from smallest element of second list
         - Add absolute value of the difference to distance
         - Remove both these elements from the list

Easy peasy.

The next part required me to create a multipler for every element of the first list occuring multiple times in the second list, and then adding the distance:

for (String dist1 : list1) {
  int multiplier = 0;
    for (String dist2 : list2) {
      if (dist1.equals(dist2)) {
        multiplier++;
      }
    }
  similarityList.add(multiplier * Integer.parseInt(dist1));
}

Day 2

I spent most of the day figuring out the bug in my solutions.

The question

Part 1 of the question gave a couple of constraints on a list of numbers, to mark them as safe. We were given multiple such lists.
My answer was off by 10 and was fixed when a friend helped me visualise the solution through a different approach. Instead of writing if-if-if-if-if-if-if-if-if-if conditions, this time I wrote cleaner, more straightforward conditions such as:

for (int i = 1; i < singleReport.size() - 1; i++) {
  int tempFirst = singleReport.get(i);
  int tempSecond = singleReport.get(i + 1);
  int tempDifference = tempFirst - tempSecond;
  if (tempDifference <= 0 || tempDifference > 3) {
    unsafe++;
    break;
  }
}

Even though I then got stuck on the second part for a while I persevered through and figured it out by the end of the day. I ended up re-writing the ‘marking safe’ logic again, in a function this time so I could re-use it for the ‘problem dampening’ system of the question.

int unsafe = 0;
for (List<Integer> report : fullReport) {
  boolean isSafe = isSafe(report);
  if (!isSafe) {
    for (int i = 0; i < report.size() - 1; i++) {
      int removed = report.remove(i);
      if (isSafe(report)) {
        break;
      }
      report.add(i, removed);
      if (i == report.size() - 1) {
        unsafe++;
      }
    }
  }
}

This solution tolerates one unsafe level as per the question and marks the report as safe. Question - Day 2: Red-Nosed Reports