Created
December 9, 2017 14:30
-
-
Save Dierk/56396737ba90dca2ce36e23e1b817a84 to your computer and use it in GitHub Desktop.
Advent of Groovy code, day 9
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
// see challenge here: http://adventofcode.com/2017/day/9 | |
String input = /your input here/ | |
String collapseEscape = input.replaceAll(/!./,'') | |
String collapseGarbage = collapseEscape.replaceAll(/<.*?>/,'') | |
String listString = collapseGarbage.tr('{}','[]') | |
String normCode = listString.replaceAll(/\[,/,'[') | |
def roseTree = evaluate(normCode) | |
def dfsVisit(node, Integer depth, Closure todo) { | |
todo(depth) // could be fused but I like the separation | |
for (child in node) { dfsVisit child, depth+1, todo} | |
} | |
def result = 0 | |
dfsVisit(roseTree, 1) { depth -> result += depth } | |
println result |
thanks for the comment and the kind words!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Doesn't work for input of: String input = {garbage, garbage, garbage,garbage}
Changing String normCode = listString.replaceAll(/[,/,'[')
to String normCode = listString.replaceAll(/[,*+/,'[') works
I love your frege examples, btw.