Advertisement
Guest User

complex-iterator.c

a guest
Jun 22nd, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.02 KB | None | 0 0
  1. /**
  2.   [2018-06-22] Challenge #364 [Hard] Tiling with Pentominos
  3.   /r/dailyprogrammer https://redd.it/8t4440
  4.  
  5.   --> NOT A FULL SOLUTION! block flip/rotation experiment. <--
  6.  
  7.   This totally shouldn't be done this way...
  8.   It actually works but way way too messy!
  9.   Attempted full solution based on this was ugly and failed.
  10. **/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. typedef struct { int mark; int width; int heigth; char flip_rots[9]; char *pattern; } pattern_t;
  16.  
  17. pattern_t pats[] = {
  18.      { 'a', 5, 1, "1...5...", "aaaaa" },
  19.      { 'b', 3, 3, "1.......", ".b$bbb$.b" },
  20.      { 'c', 3, 3, "12..56..", ".cc$.c$cc" },
  21.      { 'd', 3, 3, "1234....", "d$d$ddd" },
  22.      { 'e', 3, 3, "1.3.56..", "eee$.e$.e" },
  23.      { 'f', 3, 3, "1234....", "f$ff$.ff" },
  24.      { 'g', 3, 2, "1.3.56..", "ggg$g.g" },
  25.      { 'h', 4, 2, "12345678", "h$hhhh" },
  26.      { 'i', 4, 2, "12345678", "..ii$iii" },
  27.      { 'j', 4, 2, "12345678", "..j$jjjj" },
  28.      { 'k', 3, 3, "12345678", ".k$kkk$..k" },
  29.      { 'l', 2, 3, "12345678", "ll$ll$.l" }
  30. };
  31.  
  32. typedef struct { int done; int x; int y; int startx; int endx; int starty; int endy; int rot; char *cmd; } itr_t;
  33.  
  34. itr_t mk_itr(char r, pattern_t pat) {
  35.     itr_t ret;
  36.     int w = pat.width - 1;
  37.     int h = pat.heigth - 1;
  38.     char *c = pat.pattern;
  39.     switch(r) {                // d  X  Y bx ex by ey  r  c
  40.         case '1' : ret = (itr_t){ 0, 0, 0, 0, w, 0, h, 0, c }; break;
  41.     case '2' : ret = (itr_t){ 0, w, 0, w, 0, 0, h, 0, c }; break;
  42.     case '3' : ret = (itr_t){ 0, 0, h, 0, w, h, 0, 0, c }; break;
  43.     case '4' : ret = (itr_t){ 0, w, h, w, 0, h, 0, 0, c }; break;
  44.  
  45.         case '5' : ret = (itr_t){ 0, 0, 0, 0, h, 0, w, 1, c }; break;
  46.     case '6' : ret = (itr_t){ 0, 0, w, 0, h, w, 0, 1, c }; break;
  47.     case '7' : ret = (itr_t){ 0, h, 0, h, 0, 0, w, 1, c }; break;
  48.     case '8' : ret = (itr_t){ 0, h, w, h, 0, w, 0, 1, c }; break;
  49.     default : fprintf(stderr, "wrong way! no flip_rots orientation '%c' (0x%02x)\n", r, r);
  50.           exit(1);
  51.     };
  52.     return ret;
  53. }
  54.  
  55. void itr_step(itr_t *it) {
  56.     int dx = (it->endx > it->startx) ? +1 : -1;
  57.     int dy = (it->endy > it->starty) ? +1 : -1;
  58.     if(it->rot) {
  59.         if(*it->cmd == '$') { it->y = it->starty; it->x += dx; }
  60.     else { it->y += dy; }
  61.     }
  62.     else {
  63.         if(*it->cmd == '$') { it->x = it->startx; it->y += dy; }
  64.     else { it->x += dx; }
  65.     }
  66.     if(*it->cmd == '\0') it->done = 1;
  67.     else it->cmd++;
  68.     return;
  69. }
  70.  
  71. int main() {
  72.     int p, r, i, j;
  73.     char a[5][5];
  74.     itr_t it;
  75.     for(p = 0; p < 12; p++) {
  76.         for(r = '1'; r <= '8'; r++) {
  77.             for(i = 0; i < 5; i++) for(j = 0; j < 5; j++) a[i][j] = '.';
  78.  
  79.         // try our complex iterator...
  80.         it = mk_itr(r, pats[p]);
  81.         for(itr_t it = mk_itr(r, pats[p]); !it.done; itr_step(&it)) {
  82.             if(*it.cmd != '.' && *it.cmd != '$') a[it.x][it.y] = *it.cmd;
  83.         }
  84.  
  85.         // print the result
  86.         for(j = 0; j < 5; j++) {
  87.             for(i = 0; i < 5; i++) putc(a[i][j], stdout);
  88.         putc('\n', stdout);
  89.         }
  90.         putc('\n', stdout);
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement