2 ms·
An example of a small Prolog program used in a perfect use-case: the advent of code of this year. https://github.com/nitnelave/advent_of_code_2019/tree/master/
by nitnelave 7y ago
An example of a small Prolog program used in a perfect use-case: the advent of code of this year.
https://github.com/nitnelave/advent_of_code_2019/tree/master/04 https://github.com/nitnelave/advent_of_code_2019/tree/master...
I'm doing the advent of code in a different language every day, and since the day 4 was about some constraint resolution, Prolog was the perfect tool there.
- tom_mellior 7y agoNice. As a hint, you can unify free variables, i.e., variables not bound to anything. Future bindings of those variables will respect the equality. This means that you could write your password/1 predicate as something more like: password(X) :- length(X, 6), has_2_equal_consecutive_digits(X), has_6_digits(X), is_value_in_range(X), digits_are_increasing(X). That is, even before filling the list with digits, you would already have captured the constraint that two digits will have to be consecutive. Then, while instantiating the list inside has_6_digits/1, something like 134567 would never even be generated, and you wouldn't need to test it for being in range and having increasing digits.
- Tankenstein 7y agoAmazing, am doing the same, also chose prolog for day 4. Ended up with quite different code though: https://uku.dev/posts/advent-of-languages-day-4-prolog https://uku.dev/posts/advent-of-languages-day-4-prolog