60 lines
1.7 KiB
Text
60 lines
1.7 KiB
Text
summarize = proc (s: string) returns (string) signals (bad_format)
|
|
digit_count: array[int] := array[int]$fill(0,10,0)
|
|
for c: char in string$chars(s) do
|
|
d: int := int$parse(string$c2s(c)) resignal bad_format
|
|
digit_count[d] := digit_count[d] + 1
|
|
end
|
|
|
|
out: stream := stream$create_output()
|
|
for d: int in int$from_to_by(9,0,-1) do
|
|
if digit_count[d]>0 then
|
|
stream$puts(out, int$unparse(digit_count[d]))
|
|
stream$puts(out, int$unparse(d))
|
|
end
|
|
end
|
|
|
|
return(stream$get_contents(out))
|
|
end summarize
|
|
|
|
converge = proc (s: string) returns (int) signals (bad_format)
|
|
seen: array[string] := array[string]$[]
|
|
steps: int := 0
|
|
while true do
|
|
for str: string in array[string]$elements(seen) do
|
|
if str = s then return(steps) end
|
|
end
|
|
array[string]$addh(seen, s)
|
|
s := summarize(s)
|
|
steps := steps + 1
|
|
end
|
|
end converge
|
|
|
|
start_up = proc ()
|
|
po: stream := stream$primary_output()
|
|
|
|
seeds: array[int]
|
|
max: int := 0
|
|
for i: int in int$from_to(1, 999999) do
|
|
steps: int := converge(int$unparse(i))
|
|
if steps > max then
|
|
max := steps
|
|
seeds := array[int]$[i]
|
|
elseif steps = max then
|
|
array[int]$addh(seeds,i)
|
|
end
|
|
end
|
|
|
|
stream$puts(po, "Seed values: ")
|
|
for i: int in array[int]$elements(seeds) do
|
|
stream$puts(po, int$unparse(i) || " ")
|
|
end
|
|
|
|
stream$putl(po, "\nIterations: " || int$unparse(max))
|
|
stream$putl(po, "\nSequence: ")
|
|
|
|
s: string := int$unparse(array[int]$bottom(seeds))
|
|
for i: int in int$from_to(1, max) do
|
|
stream$putl(po, s)
|
|
s := summarize(s)
|
|
end
|
|
end start_up
|