Almost working version of algorithm, last line doesn't get added but looks otherwise promising. Also added debugging info printing and realized that printing of puzzle isn't working either.