Code below is solving <https://adventofcode.com/2015/day/10>.
When use recursion call, It used a lot of memory. I tried use input as var, but
still on effect. Have to change code to iterative to solve the memory problem.
Any idea why recusion call use so much memory, is there some mistake in my code?
import strformat, strutils, sequtils
proc findDiffCharIdx(input: string):int =
let first = input[0]
for i, c in input:
if c != first:
return i
# all the same, diff is len
return input.len
# too large mem
# proc lookAndSay(input: string, acc:string):string =
# if input.len < 1:
# return acc
# let idx = findDiffCharIdx(input)
# let num = idx
# let start = fmt"{num}{input[0]}"
# return lookAndSay(input[idx..^1], acc & start)
# same mem, tail recursion not release mem?
# proc lookAndSay(input: var string, pos:int, acc: var seq[string]) =
# if pos == input.len:
# return
# let idx = findDiffCharIdx(input[pos..^1])
# let num = idx
# let start = fmt"{num}{input[pos]}"
# acc.add(start)
# lookAndSay(input, pos+num, acc)
proc lookAndSay(input: var string, acc: var seq[string]) =
var pos = 0
while pos < input.len:
let idx = findDiffCharIdx(input[pos..^1])
let num = idx
let start = fmt"{num}{input[pos]}"
acc.add(start)
pos += num
proc main() =
var input = "3113322113"
for i in 0..<50:
var acc: seq[string] = @[]
lookAndSay(input, acc)
input = acc.join()
echo i , " ", input.len
echo fmt"output {input.len}"
main()
Run