Figure out the simple equations that can be made from the time on a digital clock
My 9-year old daughter Chloe and I like to look at the digital clock sometimes and make equations out of the numbers. For example, 12:02 could be 1*2+0 = 2, and 12:03 could be 1+2 = 0+3.
I thought it would be fun to code up a program to find all the "clock eauations". There are two parts: generating the equations, and evaluating their truth value. Generating the equations done one of two ways:
-
clock.rb uses a sequence of
.lazyenumerators to generate the times, build all possible equations from the numbers of each tine, and filter the valid equations. -
enum_clock.rb uses explicit enumerators of various kinds, mostly to see how things weould come out if I coded them up;
- Enumerator
- Standard Ruby Enumerator
- FiberEnumerator
- Enumerator using Fiber (fiber_enumerator.rb). This is
straightforward.
- CallccEnumerator
- Enumerator using bare callcc (callcc_enumerator.rb). This one is not particularly nice or well-done.
- CoroutineEnumerator
- Enumerator that encapsulates the callcc usage into a Coroutine class. This one is pretty good. (coroutine_enumerator.rb, coroutine.rb)
The equations' truth values are determined using a recursive descent expression evaluator.