Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created December 9, 2017 14:30
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 Dierk/56396737ba90dca2ce36e23e1b817a84 to your computer and use it in GitHub Desktop.
Save Dierk/56396737ba90dca2ce36e23e1b817a84 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

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