solution for 187 in lua

slow. needs improvement. works.