Skip to content

Instantly share code, notes, and snippets.

@mgritter
Last active March 26, 2018 04:28
Show Gist options
  • Save mgritter/619aff99f7c099bce787ccec4ddfb3f1 to your computer and use it in GitHub Desktop.
Save mgritter/619aff99f7c099bce787ccec4ddfb3f1 to your computer and use it in GitHub Desktop.
var shuffleGrammar = {
"origin": "#buildstack##element##element##element##element##element##element##element##element##element#",
"element" : "#stack#[stack:POP]",
"e1" : "[stack:1][e1:]",
"e2" : "[stack:2][e2:]",
"e3" : "[stack:3][e3:]",
"e4" : "[stack:4][e4:]",
"e5" : "[stack:5][e5:]",
"e6" : "[stack:6][e6:]",
"e7" : "[stack:7][e7:]",
"e8" : "[stack:8][e8:]",
"e9" : "[stack:9][e9:]",
"buildstack" : "#bs16##bs16##bs16##bs16#",
"bs16" : "#bs4##bs4##bs4##bs4#",
"bs4" : "#elements##elements##elements##elements#",
"elements" : [ "#e1#", "#e2#", "#e3#", "#e4#", "#e5#",
"#e6#", "#e7#", "#e8#", "#e9#" ]
};
var shuffleGrammar = {
"origin": "#initcounter##buildstack##element##element##element##element##element##element##element##element##element#",
"element" : "#stack#[stack:POP]",
"counter" : "[bs4:][bs16:][buildstack:]",
"initcounter" : "[counter:][counter:][counter:][counter:][counter:][counter:][counter:][counter:][counter:]",
"e1" : "[stack:1][e1:][counter:POP]",
"e2" : "[stack:2][e2:][counter:POP]",
"e3" : "[stack:3][e3:][counter:POP]",
"e4" : "[stack:4][e4:][counter:POP]",
"e5" : "[stack:5][e5:][counter:POP]",
"e6" : "[stack:6][e6:][counter:POP]",
"e7" : "[stack:7][e7:][counter:POP]",
"e8" : "[stack:8][e8:][counter:POP]",
"e9" : "[stack:9][e9:][counter:POP]",
"buildstack" : "#bs16##bs16##bs16##bs16#",
"bs16" : "#bs4##bs4##bs4##bs4#",
"bs4" : "#bs1##bs1##bs1##bs1#",
"bs1" : "X#elements##counter#",
"elements" : [ "#e1#", "#e2#", "#e3#", "#e4#", "#e5#",
"#e6#", "#e7#", "#e8#", "#e9#" ]
};
@mgritter
Copy link
Author

Challenge: write a Tracery grammar that outputs any permutation of 123456789, chosen at random.

My solutions here take a probabilistic approach which will generate an error if they fail to produce a permutation (due to stack underflow.)

The second file revises the approach a little bit to terminate immediately after adding the 9th element to the stack.

@mgritter
Copy link
Author

We can simplify the second file to use an infinite loop instead:

"buildstack" : ".#elements##counter##buildstack#",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment