Advertisement
B1KMusic

[CG] MadKnight's Fibonacci String Bit Sum Challenge Solution

Sep 10th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int
  5. fib_get(int n)
  6. {
  7.     static int fib[21] = { 0, 1 }; // fib(21) = 10946, fib(20) = 6765, max string length * bytesize = 8000
  8.     static int fi = 2;
  9.    
  10.     if(n == 0)
  11.         return 0;
  12.  
  13.     while(!fib[n])
  14.         fib[fi++] = fib[fi - 2] + fib[fi - 3];
  15.        
  16.     return fib[n];
  17. }
  18.  
  19. char *
  20. ch_to_bin(char *buf, char ch)
  21. {
  22.     for(int i = 7; i >= 0; i--, buf++)
  23.         *buf = ch & 1 << i ? '1' : '0';
  24.        
  25.     return buf;
  26. }
  27.  
  28. void
  29. build(char *buf, char *src)
  30. {
  31.     while(*src)
  32.         buf = ch_to_bin(buf, *(src++));
  33. }
  34.  
  35. int
  36. is_fib(size_t n)
  37. {
  38.     for(int i = 0; i <= 21; i++)
  39.         if(n == fib_get(i))
  40.             return 1;
  41.            
  42.     return 0;
  43. }
  44.  
  45. void
  46. parse(char *src)
  47. {    
  48.     int total = 0;
  49.    
  50.     for(int i = strlen(src) - 1; i >= 0; i--)
  51.         if(is_fib(i))
  52.             total += src[i] - '0';
  53.            
  54.     printf("%u\n", total);
  55. }
  56.  
  57. int
  58. main()
  59. {
  60.     char text[502],
  61.          binary[8001] = ""; // max string length * bytesize = 8000
  62.  
  63.     fgets(text, 501, stdin);
  64.     build(binary, text);
  65.     parse(binary);
  66.     return 0;
  67. }
  68.  
  69. /*
  70. FIB sequence = [0 1 2 3 5 8 ...]
  71.  
  72. "testcase" = 74 65 73 74 63 61 73 65
  73.  
  74. 74 0111 0100
  75. 65 0110 0101
  76. 73 0111 0011
  77. 74 0111 0100
  78. 63 0110 0011
  79. 61 0110 0001
  80. 73 0111 0011
  81. 65 0110 0101
  82.  
  83. 0111 0100
  84. 0110 0101
  85. 0111 0011
  86. 0111 0100
  87. 0110 0011
  88. 0110 0001
  89. 0111 0011
  90. 0110 0101
  91. 7654 3210
  92.  
  93. 01 [1] 1 [0100]
  94. 01 [1] 0 [0101]
  95. 01 [1] 1 [0011]
  96. 01 [1] 1 [0100]
  97. 01 [1] 0 [0011]
  98. 01 [1] 0 [0001]
  99. 01 [1] 1 [0011]
  100. 01 [1] 0 [0101]
  101. 76  5  4  3210
  102.     ^     ^^^^ < add all these together
  103.  
  104. expect: 7
  105. got: 6 + 3 + 3 + 4 + 0 + 8 = 24 ... huh?
  106.  
  107. [0111 0100] 0 < 4
  108. [0110 0101] 1 < 4
  109. [0111 0011] 2 < 5
  110. [0111 0100] 3 < 4
  111.  0110 0011  4
  112. [0110 0001] 5 < 3
  113.  0111 0011  6
  114.  0110 0101  7
  115.  
  116. also more than 7. Not sure what to do. The description is too vague. Lemme try both.
  117.  
  118. 0111 0100  0 < 2 (2)
  119. 0110 0101  1 < 3 (5)
  120. 0111 0011  2 < 3 (8) !
  121. 0111 0100  3 < 4
  122. 0110 0011  4
  123. 0110 0001  5 < 3
  124. 0111 0011  6
  125. 0110 0101  7
  126.  
  127. 7654 3210
  128.   ^  ^^^^
  129.  
  130. Someone in the comments said something about a stream of bytes, so what if I...
  131.  
  132. 0111 0100 0110 0101 0111 0011 0111 0100 0110 0011 0110 0001 0111 0011 0110 0101
  133.           ^                         ^               ^         ^     ^   ^  ^^^^
  134. 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 + 1 + 1 + 0 = 7
  135.  
  136. A-ha. Now to implement...
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement