3 ms·
I wish I had the energy to dive in to a delightful segue. But: https://news.ycombinator.com/item?id=26804484 https://news.ycombinator.com/item?id=26804484 … ok
by sillysaurusx 5y ago
I wish I had the energy to dive in to a delightful segue. But: https://news.ycombinator.com/item?id=26804484 https://news.ycombinator.com/item?id=26804484
… okay, I fail at presenting why this is interesting. Let me try again.
Ha, I tricked you. The thing I want to talk about is CHOOSE and FAIL.
There’s a certain kind of program that can solve this problem in remarkably little code: non-deterministic.
Imagine you had a JavaScript statement called fail;
if (foo) { fail; }
The program continuously restarts, trying all possible combinations such that fail never executes.
It’s like pretending the code (or incidentally, the start of my comment) has supernatural powers. It can “look into the future” and set up all the variables such that it just so happens never to fail;.
Therefore, you could solve the problem like this:
for each possibility {
if possibility doesn’t spend all the tokens {
fail
}
print(possibility)
}
The program will execute and perfectly print out every solution. And that’s all the code you need.
So with that intro, I encourage you to scroll back up and read the comment chain I linked, if you’re thirsty for details.
Now I sleep. Goodnight :) fail. Just kidding, I’m awake still.
EDIT: Bah, I really did a terrible job with this. Sorry. You’re probably wondering “why not just use continue instead of fail?” and “what about CHOOSE?”
Those are reasonable questions. The magic here is that the logic can be arbitrarily complicated. You can say “if x + y != 10, then fail; print(x,y)” and it will only print pairs like 1 and 9, 2 and 8, etc. But you’re probably still wondering “why not just loop x from 1 to 10, y from 1 to 10, and skip any x+y != 10? That’s like a Python one-liner.”
… I think this is a case where I should have thought a bit more carefully before commenting, so I’ll leave this as an actual fail. But, all I can say is, non-determinism really is delightfully fascinating, and I hope I sparked one person’s curiosity about it…
… sorry? runs away like an introvert
- nhumrich 5y agoThere are libraries based on this very concept. Instead of programming the solution, you program the "rules". And the library essentially a brute force (with branch trimming) until it finds a solution for you.