omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / langtons-ant / oracle.py
| 1 | # Independent reference for "Langton's ant" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | # the black cells as a set; the heading as a complex number (up = -1j with rows growing downward) |
| 4 | black = set() |
| 5 | pos, head = complex(50, 50), -1j # real part = column, imaginary part = row |
| 6 | steps = 0 |
| 7 | while 0 <= pos.real < 100 and 0 <= pos.imag < 100: |
| 8 | if pos in black: |
| 9 | head *= -1j # left turn |
| 10 | black.remove(pos) |
| 11 | else: |
| 12 | head *= 1j # right turn |
| 13 | black.add(pos) |
| 14 | pos += head |
| 15 | steps += 1 |
| 16 | print("steps until the ant leaves the grid:", steps) |
| 17 | rows = [int(p.imag) for p in black] |
| 18 | cols = [int(p.real) for p in black] |
| 19 | print(f"black cells, rows and columns {min(rows)}..{max(rows)} {min(cols)}..{max(cols)}") |
| 20 | for r in range(min(rows), max(rows) + 1): |
| 21 | print("".join("#" if complex(c, r) in black else "." for c in range(min(cols), max(cols) + 1))) |