Skip to content

Instantly share code, notes, and snippets.

@hornc
Last active May 27, 2019
Embed
What would you like to do?
Experimental notation interpreter for https://esolangs.org/wiki/Sticks_and_Stones
-1c
a72 1c 3c
a29 -1c -3c
a7 1c c-2 2c2 1c
a3 -1c 8c8
a-67 -8c-8 -3c
a-12 1c 3c
a55 -1c -3c
a24 1c 3c
a3 -1c -3c
a-6 1c 3c
a-8 -1c -3c
a-67 1c
#!/usr/bin/env python3
# https://esolangs.org/wiki/Sticks_and_Stones
# very rough interpreter, no control flow yet
# character output hack implemented
# proves arbitrary output is possible with three stones
import re
import sys
source_file = sys.argv[1]
with open(source_file, 'r') as f:
sticks = []
for line in f:
vectors = line.split()
for v in vectors:
x, s, y = re.match(r'([\-0-9\.]*)([abc├┤])([\-0-9\.]*)', v).groups()
sticks.append((s, (float(x or 0), float(y or 0))))
print("Program repr: %s" % sticks)
print("%d sticks.\n" % len(sticks))
A = (0,1.118)
B = (0.5,0)
C = (-0.5,0)
stones = {'A': A, 'B': B, 'C': C}
output_primed = False
distance = lambda a,b: abs(((b[0] - a[0])**2 + (b[1] - a[1])**2)**0.5)
touching = lambda a,b: 1 >= distance(a, b)
for stick in sticks:
if stick[0] in 'abc':
S = stick[0].upper()
stones[S] = tuple(map(sum, zip(stones[S], stick[1])))
elif stick[0] == '├':
# need a better way to store these branches...
pass
# Output hack:
if output_primed and touching(stones['B'], stones['C']):
#print("B and C are touching!")
print(chr(int(distance(stones['A'], stones['B']) - 1)), end='')
output_primed = False
elif not touching(stones['B'], stones['C']):
output_primed = True
print("\n\nFinal state:")
for k,stone in stones.items():
print("%s: %s" % (k, stone))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment