Skip to content

Instantly share code, notes, and snippets.

@robhol
Last active December 2, 2017 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robhol/f02d62100d9cfcc6403caade1d6e5cc8 to your computer and use it in GitHub Desktop.
Save robhol/f02d62100d9cfcc6403caade1d6e5cc8 to your computer and use it in GitHub Desktop.
//Core function providing the general solution for the problem
int CalcAll(string ip, Func<int, int> locateOther) =>
Enumerable.Range(0, ip.Length)
.Select(i => (a: ip[i], b: ip[locateOther(i) % ip.Length]))
.Where(t => t.a == t.b)
.Sum(t => t.a - '0');
//Specific solutions
int Calc1(string ip) => CalcAll(ip, i => i + 1);
int Calc2(string ip) => CalcAll(ip, i => i + ip.Length / 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment