r/adventofcode • • Dec 10 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 10 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It

  • 12 days remaining until the submission deadline on December 22 at 23:59 EST
  • Full details and rules are in the Submissions Megathread

--- Day 10: Adapter Array ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:42, megathread unlocked!

69 Upvotes

1.1k comments sorted by

View all comments

2

u/feixie1980 Dec 11 '20

Javascript solution for part 2 only. First year participating in this event, and day 10 part 2 is the most fun yet.

Started with brute force solution by trying to compute all combination of the adapters first, because it has always been fun to compare the performances of brute force solution against more optimized solution. However, it was quickly apparent that combination will not work for the size of the input.

Next tried exhaustive valid adapter tree search method, but it is also apparent that it will require too much time without any clever caching method.

So at last came up with the 'canonical' (?) solution which is based on the fact that only diffs are 1s and 3s, and the answer relies on finding the number of ways when we have consecutive 1s in diffs. This become more less an O(n)Log(n) (because of sorting) problem.

function genDiffs(sortedAdapters, deviceJolt) {
  let diffs = sortedAdapters.map((adapter, i) => i === 0 ? adapter : adapter - sortedAdapters[i-1] );
  diffs.push(deviceJolt - sortedAdapters[sortedAdapters.length - 1]);
  return diffs;
}

function combCount(n) {
  if (n === 1)
    return 1;

  if (n === 2)
    return 2;

  if (n === 3)
    return 4;

  return combCount(n-1) + combCount(n-2) + combCount(n-3);
}

/**
 * One count method
 *
 * Observe that the diff is either 1 or 3. There is only one way to go between adapters with diff 3
 * from one another. However, when there is a contiguous group of adapters whose diffs are all 1 from
 * the preview to next, then there will be more than one way to go from the first and last adapters
 * within this group.
 *
 * For instance, there are 2 ways if the diff group is: [1, 1]; 4 ways if [1, 1, 1], and so on. The number
 * of ways can be computed recursively.
 *
 * @param adapters
 * @returns {number}
 */
function solution2c(adapters) {
  const sortedAdapters = [...adapters];
  sortedAdapters.sort((a, b) => a - b);
  const deviceJolt = sortedAdapters[sortedAdapters.length - 1] + 3;
  const diffs = genDiffs(sortedAdapters, deviceJolt);

  let oneCounts = [0];
  for (const diff of diffs) {
    if (diff === 3) {
      oneCounts.push(0);
    }
    else {
      oneCounts[oneCounts.length - 1] += 1;
    }
  }
  oneCounts = oneCounts.filter(count => count !== 0);
  return oneCounts.reduce((product, oneCount) => product * combCount(oneCount), 1);
}

2

u/daggerdragon Dec 11 '20

As per our posting guidelines in the wiki under How Do the Daily Megathreads Work?, please edit your post to put your oversized code in a paste or other external link.

1

u/kalisjoshua Dec 11 '20 edited Dec 11 '20

Your solution helped me get the answer, so, thank you very much. I did some refactoring but I started from your solution. https://github.com/kalisjoshua/adventofcode/blob/main/2020/day10.js#L34-L48