Skip to content

Instantly share code, notes, and snippets.

@kiview
Last active December 15, 2017 09:43
Show Gist options
  • Save kiview/d4bac9a6035132b4d697952ff499df90 to your computer and use it in GitHub Desktop.
Save kiview/d4bac9a6035132b4d697952ff499df90 to your computer and use it in GitHub Desktop.
Advent of Code 2017
def calcDigitSum(String digits) {
digits.collect {it}.withIndex().collect { String entry, int i ->
def nextIndex = (i < digits.length() - 1) ? i + 1 : 0
String nextDigit = digits[nextIndex]
(entry == nextDigit) ? Integer.parseInt(entry) : 0
}.sum()
}
def calcDigitSum2(String digits) {
digits.collect {it}.withIndex().collect { String entry, int i ->
int halfway = digits.length() / 2
def nextIndex = (i + halfway) % digits.length()
String nextDigit = digits[nextIndex]
(entry == nextDigit) ? Integer.parseInt(entry) : 0
}.sum()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment