1. Си / Говнокод #25171

    +4

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    #include <stdio.h>
    #include <inttypes.h>
    #include <stddef.h>
    
    void printuint64(const uint64_t state);
    uint64_t game_of_life8x8(const uint64_t old);
    uint8_t getbit(uint64_t in, uint8_t y, uint8_t x);
    
    uint8_t getbit(uint64_t in, uint8_t y, uint8_t x)
    {
      return !!((in) & (1ULL << ((y&0b111) + (x&0b111)*8)) );
    }
    
    uint64_t setbit(uint8_t y, uint8_t x, uint8_t bit)
    {
      if (bit)
      {
        return 1ULL << ((y&0b111) + (x&0b111)*8);
      }
      return 0;
    }
    
    uint64_t game_of_life8x8(const uint64_t old)
    {
      uint64_t new_state = 0;
      #define GETBIT(y,x,val) getbit(val, y, x)
      #define SETBIT(y,x,bit) setbit(y,x,bit)
      
      #define SUMAROUND(y,x,val) ( \
        GETBIT(y+1,x,val) + GETBIT(y-1,x,val) \
      + GETBIT(y,x+1,val) + GETBIT(y,x-1,val) \
      + GETBIT(y+1,x+1,val) + GETBIT(y+1,x-1,val) \
      + GETBIT(y-1,x+1,val) + GETBIT(y-1,x-1,val) \
      )
    
      #define CELLSTATE(y,x,val) \
      (GETBIT(y,x,old) == 1 ? \
        ( ((SUMAROUND(y,x,val) == 2 ) || ( SUMAROUND(y,x,val) == 3 )) ? 1 : 0) \
        : \
        ( ( SUMAROUND(y,x,val) == 3 ) ? 1 : 0 ) \
      )
    
      #define SETCELL_SH(y,x,val) SETBIT(y,x,CELLSTATE(y,x,val))
    
      #define FILL_LINE(y) \
        SETCELL_SH(y,7,old) | SETCELL_SH(y,6,old) | SETCELL_SH(y,5,old) | \
        SETCELL_SH(y,4,old) | SETCELL_SH(y,3,old) | SETCELL_SH(y,2,old) | \
        SETCELL_SH(y,1,old) | SETCELL_SH(y,0,old)
    
      new_state = FILL_LINE(7) | FILL_LINE(6) | FILL_LINE(5)
      |  FILL_LINE(4) | FILL_LINE(3) | FILL_LINE(2)
      |  FILL_LINE(1) | FILL_LINE(0);
    
      return new_state;
    }
    
    void printuint64(const uint64_t state)
    {
      for (size_t i = 7; i != SIZE_MAX; i--)
      {
        for (size_t j = 7; j != SIZE_MAX; j--)
        {
          printf("%u", !!((state >> (j+i*8)) & 1) );
        }
        printf("\n");
      }
    }
    
    int main(void)
    {
      // Glider
      uint64_t state =
      (( 0b01000000ULL ) << 8*7 ) |
      (( 0b00100000ULL ) << 8*6 ) |
      (( 0b11100000ULL ) << 8*5 ) |
      (( 0b00000000ULL ) << 8*4 ) |
      (( 0b00000000ULL ) << 8*3 ) |
      (( 0b00000000ULL ) << 8*2 ) |
      (( 0b00000000ULL ) << 8*1 ) |
      (( 0b00000000ULL ) << 8*0 );
    
      
      for (size_t i = 0; i < 50; i++)
      {
        printuint64(state);
        state = game_of_life8x8(state);
        printf("\n");
      }
      return 0;
    }

    Conway's game of life внутри uint64_t (8 на 8) с периодическими граничными условиями.

    Запостил: j123123, 09 Декабря 2018

    Комментарии (148) RSS

    Добавить комментарий