2 min read

Advent of Code 2020 Day 9

I also continually feel like at least for Advent of Code, I spend more time to focus on better programming practices rather than speed
Advent of Code 2020 Day 9

So, today's Advent of Code Puzzle was actually pretty interesting and involved some fairly interesting data processing techniques. I can't say I really used streams well again, but this all kind of goes back to what I was talking about in yesterday's blog about how asynchronous or state-based looping is just challenging to do in streams, because it kind of just does the same operation on every element in a list unless you filter, but then there are situations where you want to bisect a stream and split it into two different streams based on conditions (not allowed) or do some out-of-stream collecting, which is discouraged by the API documentation. Those instances are where I just move to the old reliable for loop and while loops.

Nothing in this day's puzzle really stood out as stumping me other than just making me think.  Knowing how to reduce lookup times was pretty key to my success.  Rather than iterate through all of the "queue" of potential values, I instead used a stream to determine if the sum of two values existing within the queue of data.

public static long processValues(List<Long> data, int size){
  Queue<Long> queue = new ArrayDequeue<Long>(size);
  for(Long value : data){
    if(queue.size() < size) {
      queue.add(value);
    } else {
      if(queue.stream()
              .filter(qVal -> queue.contains(value - qVal))
              .count() == 0){
        return value;
      }
      queue.remove();
      queue.add(value);
    }
  }
}

This is the logic that 1) built the queue, 2) processed the running window queue to see if the value after the queue was a sum of 2 of the values within the queue and 3) immediately exited when the queue didn't contain the two values that summed to the value.

Part two involved looking for 2 or more consecutive numbers in the entire data that summed to the value that was found in part 1. I did the sums using streams as it was the easiest way to sum a list of numbers (at least for me) and then to find the minimum value and maximum value to then return the sum of those as the answer for part two. Based on my timing, it took about 20 minutes to solve part 2, I'd say a good portion of that was hardening up part 1 to make it easier to support the part two pieces. I also continually feel like at least for Advent of Code, I spend more time to focus on better programming practices rather than speed (which I think is better for me).

If you want to join my Advent of Code leaderboard, feel free to join with the code: 699615-aae0e8af.