Random Post
Recent Posts
- BrainTwister #19: Angular arrangements
- Enigma 767: Safety in numbers
- BrainTwister #18: The arithmetical two-step
- Enigma 774: Sting in the tail
- BrainTwister #17: Semi-one numbers
- Enigma 773: Duodecimal
- BrainTwister #16: Order, order!
- Enigma 772: Have you seen the trailer?
- BrainTwister #15: Domino strips
- Enigma 771: Cross the island
Recent Comments
Archives
Categories
- article (11)
- braintwister (19)
- enigma (1,717)
- enigma-book-1982 (70)
- headscratchers-book-2023 (70)
- microteasers-book-1986 (11)
- misc (7)
- project euler (2)
- puzzle (90)
- puzzle# (249)
- site news (83)
- sphinx (4)
- tantalizer (254)
- tantalizer-book-1970 (40)
- teaser (7)
- today (1)
Site Stats
- 347,385 hits
Here is a solution using the [[
SubstitutedExpression()
]] solver from the enigma.py library.We use the same set of expressions for each part, but change the function were are using to test adjacent digits.
It runs in 86ms. (Internal runtime is 17ms).
Run: [ @replit ]
Solution: (a) There are 30 possible arrangements, for example: (1 3 5 2 4 7 9 6 8); (b) There are 140 possible arrangements, for example: (1 2 3 4 7 6 5 8 9); (c) There is 1 possible arrangement: (7 2 8 1 6 3 5 4 9).
If part (a) had specified 3 or 7 as the difference, there would be just one solution:
3 6 9 2 5 8 1 4 7 (or the inverse). Admittedly it is not hard to find.
If the difference is to be 2 or 5 there are four solutions (and their inverses).
Here’s one brute force solution, which runs slowly (but still within 30 seconds) and a much faster solution (runs in 30 ms on my iPad).
Note that I use my -just released- istr module that makes it possible to use strings as if they were integers in calcuations. This can be quite handy in solving several enigmas. You can find the extensive readme on http://www.github.com/salabim/istr . There you’ll also find how to install from PyPI.
@Jim you could consider adding it to our enigma module: it is just one class.
Brute force:
Fast recursive version:
Thanks. I’ll have a look at
istr
.One suggestion I might make is to provide a way of iterating through the digits of an
istr
that provides the individual digits asistr
objects. Maybe:Or provide an
istr.digits()
method that does this.Thanks for your feedback.
I have implemented a __iter__ method, albeit implemented slightly different from your suggestion.
And I have added a new (class) method digits. So now we can do:
See PyPI or Github for details.
Three programs for parts (a), (b) and (c)
Part(a)
Part(b)
Part (c)