Last active
December 15, 2017 09:43
-
-
Save kiview/d4bac9a6035132b4d697952ff499df90 to your computer and use it in GitHub Desktop.
Advent of Code 2017
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
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() | |
} |
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
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