interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / towers-of-hanoi / towers-of-hanoi.om

51 lines · 937 bytes Raw Blame History
1note Rosetta Code task "Towers of Hanoi": https://rosettacode.org/wiki/Towers_of_Hanoi
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text t1 "Move disk "
6text t2 " from pole "
7text t3 " to pole "
8note emit writes every byte of a to stdout; a short write is retried, an error exits 1
9fn emit a view uchar * to void
10o = ulong 0
11while lt o len a bound 4096
12k = write 1 a o sub len a o
13if le k 0
14exit 1
15end
16o = add o ulong k
17end
18end
19note emitu writes n in decimal; 20 digits hold every ulong
20fn emitu n ulong to void
21d = new uchar 20 0
22i = ulong 20
23v = n
24while true bound 20
25i = sub i 1
26put d i uchar add rem v 10 48
27v = div v 10
28if eq v 0
29break
30end
31end
32emit part d i sub 20 i
33end
34fn hanoi n ulong src ulong dst ulong via ulong to void
35if eq n 0
36ret
37end
38hanoi sub n 1 src via dst
39emit t1
40emitu n
41emit t2
42emitu src
43emit t3
44emitu dst
45emit nl
46hanoi sub n 1 via dst src
47end
48fn main to int
49hanoi 4 1 3 2
50ret 0
51end