Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/langtons-ant/langtons-ant.om # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / langtons-ant / langtons-ant.om` 140 lines ยท 2529 bytes Raw Blame History 1 note Rosetta Code task "Langton's ant": https://rosettacode.org/wiki/Langton's_ant 2 note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 3 om2 4 text nl "\n" 5 text sp " " 6 text minus "-" 7 text tsteps "steps until the ant leaves the grid: " 8 text tbox "black cells, rows and columns " 9 text tto ".." 10 text tdot "." 11 text thash "#" 12 global grid uchar 10000 13 global box ulong 4 14 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 15 fn emit a view uchar * to void 16 o = ulong 0 17 while lt o len a bound 4096 18 k = write 1 a o sub len a o 19 if le k 0 20 exit 1 21 end 22 o = add o ulong k 23 end 24 end 25 note emitu writes n in decimal; 20 digits hold every ulong 26 fn emitu n ulong to void 27 d = new uchar 20 0 28 i = ulong 20 29 v = n 30 while true bound 20 31 i = sub i 1 32 put d i uchar add rem v 10 48 33 v = div v 10 34 if eq v 0 35 break 36 end 37 end 38 emit part d i sub 20 i 39 end 40 note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black 41 note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left 42 fn walk to ulong 43 r = long 50 44 c = long 50 45 d = ulong 0 46 s = ulong 0 47 note the ant is known to leave a grid this size after about 12000 steps (the highway starts near 48 note step 10000), so 999999 is a safe bound 49 while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999 50 k = add mul ulong r 100 ulong c 51 if eq get grid k 0 52 d = rem add d 1 4 53 put grid k 1 54 else 55 d = rem add d 3 4 56 put grid k 0 57 end 58 r = add r dy d 59 c = add c dx d 60 s = add s 1 61 end 62 ret s 63 end 64 fn dy d ulong to long 65 if eq d 0 66 ret -1 67 end 68 if eq d 2 69 ret 1 70 end 71 ret 0 72 end 73 fn dx d ulong to long 74 if eq d 1 75 ret 1 76 end 77 if eq d 3 78 ret -1 79 end 80 ret 0 81 end 82 note bounds finds the smallest box holding every black cell: box = top, bottom, left, right 83 fn bounds to void 84 put box 0 99 85 put box 1 0 86 put box 2 99 87 put box 3 0 88 for r ulong 100 89 for c ulong 100 90 if eq get grid add mul r 100 c 1 91 grow r c 92 end 93 end 94 end 95 end 96 fn grow r ulong c ulong to void 97 if lt r get box 0 98 put box 0 r 99 end 100 if gt r get box 1 101 put box 1 r 102 end 103 if lt c get box 2 104 put box 2 c 105 end 106 if gt c get box 3 107 put box 3 c 108 end 109 end 110 note row prints grid row r between the box's left and right columns 111 fn row r ulong to void 112 for x ulong add sub get box 3 get box 2 1 113 c = add x get box 2 114 if eq get grid add mul r 100 c 1 115 emit thash 116 else 117 emit tdot 118 end 119 end 120 emit nl 121 end 122 fn main to int 123 emit tsteps 124 emitu walk 125 emit nl 126 bounds 127 emit tbox 128 emitu get box 0 129 emit tto 130 emitu get box 1 131 emit sp 132 emitu get box 2 133 emit tto 134 emitu get box 3 135 emit nl 136 for r ulong add sub get box 1 get box 0 1 137 row add r get box 0 138 end 139 ret 0 140 end