omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / langtons-ant.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text tsteps "steps until the ant leaves the grid: " |
| 10 | text tbox "black cells, rows and columns " |
| 11 | text tto ".." |
| 12 | text tdot "." |
| 13 | text thash "#" |
| 14 | global grid uchar 10000 |
| 15 | global box ulong 4 |
| 16 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 17 | fn emit a view uchar * to void |
| 18 | o = ulong 0 |
| 19 | while lt o len a bound 4096 |
| 20 | k = write 1 a o sub len a o |
| 21 | if le k 0 |
| 22 | exit 1 |
| 23 | end |
| 24 | o = add o ulong k |
| 25 | end |
| 26 | end |
| 27 | note emitu writes n in decimal; 20 digits hold every ulong |
| 28 | fn emitu n ulong to void |
| 29 | d = new uchar 20 0 |
| 30 | i = ulong 20 |
| 31 | v = n |
| 32 | while true bound 20 |
| 33 | i = sub i 1 |
| 34 | put d i uchar add rem v 10 48 |
| 35 | v = div v 10 |
| 36 | if eq v 0 |
| 37 | break |
| 38 | end |
| 39 | end |
| 40 | emit part d i sub 20 i |
| 41 | end |
| 42 | note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black |
| 43 | note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left |
| 44 | fn walk to ulong |
| 45 | r = long 50 |
| 46 | c = long 50 |
| 47 | d = ulong 0 |
| 48 | s = ulong 0 |
| 49 | note the ant is known to leave a grid this size after about 12000 steps (the highway starts near |
| 50 | note step 10000), so 999999 is a safe bound |
| 51 | while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999 |
| 52 | k = add mul ulong r 100 ulong c |
| 53 | if eq get grid k 0 |
| 54 | d = rem add d 1 4 |
| 55 | put grid k 1 |
| 56 | else |
| 57 | d = rem add d 3 4 |
| 58 | put grid k 0 |
| 59 | end |
| 60 | r = add r dy d |
| 61 | c = add c dx d |
| 62 | s = add s 1 |
| 63 | end |
| 64 | ret s |
| 65 | end |
| 66 | fn dy d ulong to long |
| 67 | if eq d 0 |
| 68 | ret -1 |
| 69 | end |
| 70 | if eq d 2 |
| 71 | ret 1 |
| 72 | end |
| 73 | ret 0 |
| 74 | end |
| 75 | fn dx d ulong to long |
| 76 | if eq d 1 |
| 77 | ret 1 |
| 78 | end |
| 79 | if eq d 3 |
| 80 | ret -1 |
| 81 | end |
| 82 | ret 0 |
| 83 | end |
| 84 | note bounds finds the smallest box holding every black cell: box = top, bottom, left, right |
| 85 | fn bounds to void |
| 86 | put box 0 99 |
| 87 | put box 1 0 |
| 88 | put box 2 99 |
| 89 | put box 3 0 |
| 90 | for r ulong 100 |
| 91 | for c ulong 100 |
| 92 | if eq get grid add mul r 100 c 1 |
| 93 | grow r c |
| 94 | end |
| 95 | end |
| 96 | end |
| 97 | end |
| 98 | fn grow r ulong c ulong to void |
| 99 | if lt r get box 0 |
| 100 | put box 0 r |
| 101 | end |
| 102 | if gt r get box 1 |
| 103 | put box 1 r |
| 104 | end |
| 105 | if lt c get box 2 |
| 106 | put box 2 c |
| 107 | end |
| 108 | if gt c get box 3 |
| 109 | put box 3 c |
| 110 | end |
| 111 | end |
| 112 | note row prints grid row r between the box's left and right columns |
| 113 | fn row r ulong to void |
| 114 | for x ulong add sub get box 3 get box 2 1 |
| 115 | c = add x get box 2 |
| 116 | if eq get grid add mul r 100 c 1 |
| 117 | emit thash |
| 118 | else |
| 119 | emit tdot |
| 120 | end |
| 121 | end |
| 122 | emit nl |
| 123 | end |
| 124 | fn main to int |
| 125 | emit tsteps |
| 126 | emitu walk |
| 127 | emit nl |
| 128 | bounds |
| 129 | emit tbox |
| 130 | emitu get box 0 |
| 131 | emit tto |
| 132 | emitu get box 1 |
| 133 | emit sp |
| 134 | emitu get box 2 |
| 135 | emit tto |
| 136 | emitu get box 3 |
| 137 | emit nl |
| 138 | for r ulong add sub get box 1 get box 0 1 |
| 139 | row add r get box 0 |
| 140 | end |
| 141 | ret 0 |
| 142 | end |
| 143 | </syntaxhighlight> |
| 144 | {{out}} |
| 145 | <pre> |
| 146 | steps until the ant leaves the grid: 11669 |
| 147 | black cells, rows and columns 28..78 0..79 |
| 148 | ..........................................##..############..##.................. |
| 149 | .........................................#..####..........#..##................. |
| 150 | ........................................###...##............##.#................ |
| 151 | ........................................#.#..#.........#..#....#................ |
| 152 | ....................................##..##.#.#.........###.......#.............. |
| 153 | .................................###.#..#...#.....#.....##.##..###.............. |
| 154 | ..................................#.#..###..##.####.##...#.#..#.##..##.......... |
| 155 | ..................................#.###.##..#.##..###.#.#.....###...###......... |
| 156 | ................................#.....#...#####.#.#..####..#...###.#.#.#........ |
| 157 | ...............................###.##...#.####..##.##.######.#.###.#...#........ |
| 158 | ...............................#.###.#.##.#.#.##.##.##.#...#####.###.##......... |
| 159 | ...................................#.#...#.##.###...#...#.#..####....#.##....... |
| 160 | ................................#..#.........##.##...#..##.....##.#.....##...... |
| 161 | ... |
| 162 | .###...#..##........................................................##......##.. |
| 163 | #..###..#.#..........................................................##......... |
| 164 | .###.#..##...................................................................... |
| 165 | #.#.#.#.#....................................................................... |
| 166 | .####.##........................................................................ |
| 167 | .#.##.#......................................................................... |
| 168 | ..####.......................................................................... |
| 169 | ...##........................................................................... |
| 170 | </pre> |