[chbot] New chocolate fish challenge

Andre Renaud andre at bluewatersys.com
Wed Nov 19 04:35:32 GMT 2014


Hi Mike,
Are you allowed loops? You can implement addition using <<, &, ^. So:

static int inc(int x)
{
        int y = 1;
        while (y) {
                int carry = x & y;
                x = x ^ y;
                y = carry << 1;
        }
        return x;
}

int fish(void)
{
        static unsigned char b = 0;

        b = inc(b);
        if (b == 60) {
                b = 0;
                return 1;
        }
        return 0;
}

More than 10 lines though. Essentially it's just the naive solution
with b++ 'unpacked' into binary operators.

Andre

On 19 November 2014 15:36, hamster <hamster at snap.net.nz> wrote:
> I'm building a jumbo LED clock (using 8" 7-segment displays) for the local
> pool, as I can't read a small analogue clock without my glasses when
> swimming. The PCBs are on their way from China as I type....
>
> So here is the challenge. Make a C function that usually returns 0, but
> returns a value of 1 once in every 60 calls calls - just like this:
>
> int chocolate_fish(void)
>
> {
>
>   unsigned counter = 0;
>
>    if(counter == 59) {
>
>       counter = 0;
>
>       return 1;
>
>    }
>
>    counter++;
>
>    return 0;
>
> }
>
> Now to make it hard. You can't use any math operations - no addition,
> subtraction multiplication or division, including the increment/decrement
> operations.
>
> Bonus points for using no more static memory than one byte, and code less
> than 10 lines - with each statement on a line of their own.
>
> Mike
>
>
>
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.linuxnut.co.nz
> http://lists.ourshack.com/mailman/listinfo/chchrobotics
> Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> Web site: http://kiwibots.org
> Meetings 3rd Monday each month.
>
> When replying, please edit your Subject line to reflect new content.



-- 
Bluewater Systems - An Aiotec Company

Andre Renaud
andre at bluewatersys.com          5 Amuri Park, 404 Barbadoes St
www.bluewatersys.com            PO Box 13 889, Christchurch 8013
www.aiotec.co.nz                New Zealand
Phone: +64 3 3779127            Freecall: Australia 1800 148 751
Fax:   +64 3 3779135            USA 1800 261 2934



More information about the Chchrobotics mailing list