Last active
December 17, 2020 14:34
-
-
Save andrewboudreau/911e92fc4b7b7f0c6a74f809018acb05 to your computer and use it in GitHub Desktop.
advent of code 2020 day 10 part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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