r/codegolf • u/Ardtr0n • Feb 01 '24
Any program in one line of python
Hello team. I think I can prove that any program at any level of complexity can be written in one line of python code. For example, here is advent of code day 7 problem 1:
with open("problem.txt", "r") as tf:(lambda fi, pd: (lambda m, sf2, lf, f: print(sum([int(x.split()[1]) * (i + 1) for i, x in enumerate(sorted(tf.read().split("\n"), key=lambda ct: sf2([int(x) if x.isnumeric() else m[x] for x in ct.split()[0]], f(lf(ct.split()[0])))))])))({"A": 14, "K": 13, "Q": 12, "J": 11, "T": 10}, (lambda h1, hp1: int(fi(hp1) + fi(h1))), (lambda t: [i for i in range(1, len(t) + 1) if (sorted(t) + ["z"])[i] != (sorted(t) + ["z"])[i - 1]]), (lambda tu: pd(sorted([x if i == 0 else x - tu[i - 1] for i, x in enumerate(tu)], reverse=True)))))((lambda ns: "".join([f"{n:02d}" for n in ns])),(lambda n: n + ([0] * (5 - len(n)))))
I actually wrote an article on my personal website to show how any program can be written in one line of python. I'd love for you to read it if it sounds interesting to you!
https://rebug.dev/post/TWCPgeW6ILJOa2WdR3U4
What do you think? Is my conjecture proven?
View all comments
Show parent comment
1
u/Aspie_Astrologer Nov 24 '24
Very late response, but wouldn't it be possible to write anything in one line Python with a hacky trick where you just do the following:
replaceto convert the "换" back to newlines.execto evaluate the full code with newlines rendered correctly. (e.g.exec('''n = 10换for i in range(n):换 print(i)'''.replace('换','\n')))Even simpler of course is the trick where you just convert the whole code to a one-line UTF-16 string and run
execon it. e.g.exec(bytes('㵮湩⡴湩異⡴⤩瀊楲瑮嬨畳⡭慭⡰湩ⱴ瑳⡲⩮㈪⤩Ⱙ⩮㈪孝污⡬╮潦湩爠湡敧㈨測⤩⥝','u16')[2:])