Skip to content

Instantly share code, notes, and snippets.

@andrewboudreau
Last active December 17, 2020 14:34
Show Gist options
  • Save andrewboudreau/911e92fc4b7b7f0c6a74f809018acb05 to your computer and use it in GitHub Desktop.
Save andrewboudreau/911e92fc4b7b7f0c6a74f809018acb05 to your computer and use it in GitHub Desktop.
advent of code 2020 day 10 part 2
// Problem: Find the number of paths through a tree where each node can have 1,2 or 3 children?
string SolvePart2(IEnumerable<string> inputs)
{
var adapters = inputs.ToInts().Append(0).OrderBy(x => x).ToList();
long total = 1;
int branches = 0;
var joltage = 0;
for (var index = 1; index < adapters.Count; index++)
{
var choices = adapters.Skip(index).Count(x => x - joltage <= 3);
joltage += adapters[index] - joltage;
branches++;
if (choices == 1)
{
total *= branches switch
{
1 => 1,
2 => 2,
3 => 4,
4 => 7,
_ => throw new InvalidOperationException($"{branches} is an invalid length")
};
branches = 0;
}
}
return $"{total} possible adapter stacking options";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment