]> Pileus Git - ~andy/ct/blob - knot/knot.c
Add knot
[~andy/ct] / knot / knot.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "knot.h"
5
6 static void do_tile(tile_t **tiles, int row, int col)
7 {
8         tile_t *t = &tiles[row][col];
9         tile_t *l = &tiles[row][col-1];
10         tile_t *r = &tiles[row][col+1];
11         tile_t *u = &tiles[row-1][col];
12         //tile_t *d = &tiles[row+1][col];
13
14         /* Fill in what we know */
15         switch (tiles[row][col].c) {
16         case '-':  t->top = LEFT | RIGHT; break;
17         case '|':  t->top = UP | DOWN;    break;
18         case '\'': t->top = UP;           break;
19         case '.':  t->top = DOWN;         break;
20         }
21
22         /* Follow bottoms */
23         if (t->c == '|' && (l->top | l->bot) & RIGHT)
24                 t->bot = LEFT | RIGHT;
25         if (t->c == '-' && (u->top | u->bot) & DOWN)
26                 t->bot = UP | DOWN;
27
28         /* Adds sides for ''s and .'s */
29         if (t->c == '.' || t->c == '\'') {
30                 if ((l->top | l->bot) & RIGHT)
31                         t->top |= LEFT;
32                 else
33                         t->top |= RIGHT;
34         }
35
36 }
37
38 static void print_ptrn(int ptrn)
39 {
40         printf("%c", ptrn & LEFT  ? '<' : '-');
41         printf("%c", ptrn & RIGHT ? '>' : '-');
42         printf("%c", ptrn & UP    ? '^' : '-');
43         printf("%c", ptrn & DOWN  ? 'v' : '-');
44 }
45
46 int main()
47 {
48         /* Init tiles */
49         tile_t *tiles[64];
50         for (int i = 0; i < 64; i++)
51                 tiles[i] = calloc(512, sizeof(tile_t));
52
53         /* Read data */
54         char c;
55         int row = 1, col = 1;
56         while ((c = getchar()) != EOF) {
57                 if (row > 63 || col > 511)
58                         exit(-1);
59                 if (c == '\n') {
60                         row++;
61                         col = 1;
62                 } else {
63                         tiles[row][col].c = c;
64                         col++;
65                 }
66         }
67
68         /* Process */
69         for (row = 1; row < 63; row++)
70                 for (col = 1; col < 511; col++)
71                         do_tile(tiles, row, col);
72
73         /* Output */
74         //for (row = 1; row < 63; row++) {
75         //      for (col = 1; col < 511; col++) {
76         //              print_ptrn(tiles[row][col].top);
77         //              print_ptrn(tiles[row][col].bot);
78         //              printf(" ");
79         //      }
80         //      printf("\n");
81         //}
82
83         /* HTML */
84         print_index(tiles);
85 }