Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Last active November 4, 2022 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyNovo/273cdbdd70a9d9730b77897ee986b330 to your computer and use it in GitHub Desktop.
Save AndyNovo/273cdbdd70a9d9730b77897ee986b330 to your computer and use it in GitHub Desktop.
import copy
from PIL import Image
color = 0
cols=[[] for _ in range(300)]
idx=299
def printcols():
for col in cols:
if col != []:
print("".join(map(str,col)))
def c_op():
global color
color = (color + 1) % 2
def i_op():
global idx, cols
cols[idx].append(color)
def l_op():
global idx, cols
idx = ((idx - 1) + len(cols)) % len(cols)
def r_op():
global idx
idx = (idx + 1) % len(cols)
def d_op():
global cols, idx
cols[idx-1] = copy.copy(cols[idx])
idx=idx-1
def s_op():
global cols, idx
cols[idx+1] = cols[idx+1] + cols[idx]
cols[idx]=[]
idx=idx+1
def runcode(src):
global cols, color, idx
cols=[[] for _ in range(300)]
color=0
idx=299
for ins in src:
if ins == "c":
c_op()
elif ins == "d":
d_op()
elif ins == "i":
i_op()
elif ins == "l":
l_op()
elif ins == "r":
r_op()
elif ins == "s":
s_op()
printcols()
def makeline(n,cur=0,res=""):
if cur == n:
return res
if n == cur+1:
return res + "i"
if n == cur*2:
return res + "ds"
if cur == 0:
return makeline(n, 2, "ii")
if cur*2 < n:
return makeline(n, cur*2, res + "ds")
return makeline(n,cur+2,res+"ii")
def makecol(parts):
res="l"
if type(parts) == str:
return parts
if parts[0] < 0:
res += "c"
parts[0] = -1*parts[0]
return res + "scl".join(map(makeline, parts)) + "s"
def makespecial(pxls):
#this consumes [0, 0, 0, 1, 1, 0, 0, 0] and should return [3,2,3] and if the first element is 1 make it [-3, 2, 3]
cur = pxls[0]
count = 0
res=[]
for i in range(len(pxls)):
if pxls[i] == cur:
count += 1
else:
res.append(count)
count = 1
cur = pxls[i]
res.append(count)
if pxls[0] == 1:
res[0] = res[0]*-1
return res
def readimage(filename):
im=Image.open(filename)
colis=range(im.size[0]-1,-1,-1)
cols=[]
for i in colis:
pxls=[im.getpixel((i,j)) for j in range(im.size[1]-1, -1, -1)]
cols.append(makespecial(pxls))
return "l".join(map(makecol, cols))
code=readimage("helloworld.png")
runcode(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment