[chbot] New chocolate fish challenge
Michael Field
hamster at snap.net.nz
Wed Nov 19 09:07:12 GMT 2014
Here's a solution to the Chocolate fish challenge..
=================
#include <stdio.h>
static int chocolate_fish_challenge(void) {
static unsigned char reg = 0x22;
if(reg) {
reg = (reg >> 1) ^ ((reg & 1) ? 0 : 0x30);
return 0;
}
reg = 0x22;
return 1;
}
int main(int argc, char *argv[])
{
int i = 0;
while(1)
printf("%i %i\r\n", i++, chocolate_fish_challenge());
}
==========================
The theory behind it is interesting if you are that way inclined. As
Charles said it is a fundamental technology in how GPS, some crypto
systems and CDMA cell phones work - see
http://en.wikipedia.org/wiki/Linear_feedback_shift_register
And I guess given my history on the list I should at least prove that
for once I have actually tested that it works - here it is printing out
a couple of lines either side of where it returns '1':
$ ./main | grep -B 2 -A 2 " 1" | head -n 20
57 0
58 0
59 1
60 0
61 0
--
117 0
118 0
119 1
120 0
121 0
--
177 0
178 0
179 1
180 0
181 0
--
Mike
On 19/11/2014 7:13 p.m., Mark Atherton wrote:
> Below is a working simulation of the cascaded T type flip flop
> solution. Still not too sure about coding size constrains, I think it
> is a bit large.
>
> Of all the solutions so far, my preference is Charles Manning's PRN
> since there is not a requirement for linear counting. His idea could
> be coded quite tightly. Just a shift, an XOR or two, and when the
> terminal pattern is reached, reload the initial seed and flag the
> output. I guess the next step is to see if CMs solution can be coded
> in 10 instructions of assembler. If that fits, how about 10 bytes of
> assembler, maybe using the 6800.
>
> -mark
>
> =========================
>
> int main(void)
> {
> unsigned char val = 0;
> while(1)
> {
> val ^= 0x01;
> if((val & 0x01) == 0x00) val^= 0x02;
> if((val & 0x03) == 0x00) val^= 0x04;
> if((val & 0x07) == 0x00) val^= 0x08;
> if((val & 0x0F) == 0x00) val^= 0x10;
> if((val & 0x1F) == 0x00) val^= 0x20;
> if(val == 60)
> {
> val = 0;
> printf("BINGO\n", val);
> }
> printf("val %d\n", val);
> }
> return(0);
> }
>
> val 0
> val 1
> val 2
> val 3
> val 4
> val 5
> val 6
> val 7
> val 8
> val 9
> val 10
> val 11
> val 12
> val 13
> val 14
> val 15
> val 16
> val 17
> val 18
> val 19
> val 20
> val 21
> val 22
> val 23
> val 24
> val 25
> val 26
> val 27
> val 28
> val 29
> val 30
> val 31
> val 32
> val 33
> val 34
> val 35
> val 36
> val 37
> val 38
> val 39
> val 40
> val 41
> val 42
> val 43
> val 44
> val 45
> val 46
> val 47
> val 48
> val 49
> val 50
> val 51
> val 52
> val 53
> val 54
> val 55
> val 56
> val 57
> val 58
> val 59
> BINGO
>
>
> _______________________________________________
> 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.
>
More information about the Chchrobotics
mailing list