Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advent of Groovy code, day 9
// 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
@cchanley2003

This comment has been minimized.

cchanley2003 commented Dec 10, 2017

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.

@Dierk

This comment has been minimized.

Owner

Dierk commented Dec 13, 2017

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