|
|
|
|
|
by Marc66FR
3312 days ago
|
|
I never bothered creating a program to solve this as I prefer to do the exercise mentally. I've been playing with 1440 puzzles: one for each display of a 24 hour clock. The puzzle is simple: take a displayed time and using the 4 displayed numbers and the 4 basic operations (+ - x /), make it solve to zero. To make it more complex, it is forbidden to multiply by zero. Eg. 12:34 >> (1+4)-(2+3)=0. The puzzle does not solve for all hours, but it does for 12:3x and many other times |
|
First, let us again start with a suitable operator definition, so that we can use × as an infix operator in expressions:
Next, let us declaratively describe what we mean by a "clock", using integer constraints: For example: We can collect all solutions as a quick verification: This confirms that there are 1440 puzzles of this kind, one for each possible clock time.The puzzle now asks for expressions that evaluate to 0. We can relate any given clock display to an expression like this:
This reuses predicates I posted earlier, which I only need to slightly modify to prohibit multiplication by zero, and which I repeat here for completeness: We can use this to enumerate all times that have a solution: And here is the list of all times that don't have a solution: