4 ms·
How easy would it be to encode and solve "Einstein's Riddle" https://udel.edu/~os/riddle.html https://udel.edu/~os/riddle.html in LogicJS? I looked into startin
by stevebmark 10y ago
How easy would it be to encode and solve "Einstein's Riddle" https://udel.edu/~os/riddle.html https://udel.edu/~os/riddle.html in LogicJS? I looked into starting it but there's things like this:
> the Norwegian lives next to the blue house
Which I'm not sure how encode without a huge decision tree, or some sort of dynamic logic encoding.
- riboflava 10y agoFor reference, Clojure's core.logic: https://gist.github.com/rm-hull/6952960 https://gist.github.com/rm-hull/6952960 Or, from Norvig, presumably as an update to the solution he made with Lisp and Prolog, here's a Python version that doesn't use any fancy logic engine: https://gist.github.com/joyrexus/5301901 https://gist.github.com/joyrexus/5301901
- StavrosK 10y agoJesus, Norvig's version is so dauntingly short...
- siddboots 10y agoThe core relation involved is one that captures the characteristics for an individual house: function house(position, country, color, pet, drink, cigar); You need to define that relation by codifying the following: a) The exclusivity rule. I.e. the fact that no two houses are the same colour, and no two owners have the same pet, and so on. You'll need to define your own "not equal" relation, the absence of which is a major issue with this library. b) the positioning. Since this library can do arithmetic, I would just make position an integer, and do something like this: function left_of(pos_a, pos_b) { eq(logic.sub(pos_b, pos_a), 1) } function adjacent(pos_a, pos_b) { or(left_of(pos_a, pos_b), left_of(pos_b, pos_a)) } c) finally, just go in and codify all of the hints. This will probably be quite long and awkward because you'll need lots of lvars, and the conjunction relation is binary only, but it should be easy to do nonetheless. For example: > the Norwegian lives next to the blue house and(and( house(pos_a, 'Norwegian', col_a, pet_a, drink_a, cigar_a), house(pos_b, country_b, 'Blue', pet_b, drink_b, cigar_b)), adjacent(pos_a, pos_b) )
- sriku 10y agoI have this exact example in FD.js (https://github.com/srikumarks/FD.js https://github.com/srikumarks/FD.js), plus Sudoku solvers and others.