SHARE
TWEET

2015 Challenge24 Finals: C3 Generator by HoChockiGon

a guest Jul 1st, 2015 285 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstring>
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <set>
  7. #include <map>
  8. #include <stack>
  9. #include <list>
  10. #include <queue>
  11. #include <deque>
  12. #include <cctype>
  13. #include <string>
  14. #include <vector>
  15. #include <sstream>
  16. #include <iterator>
  17. #include <numeric>
  18. #include <cmath>
  19. using namespace std;
  20.  
  21. typedef vector <int > VI;
  22. typedef vector < VI > VVI;
  23. typedef long long LL;
  24. typedef vector < LL > VLL;
  25. typedef vector < double > VD;
  26. typedef vector < string > VS;
  27. typedef pair<int,int> PII;
  28. typedef vector <PII> VPII;
  29. typedef istringstream ISS;
  30.  
  31. #define ALL(x) x.begin(),x.end()
  32. #define REP(i,n) for (int i=0; i<(n); ++i)
  33. #define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var)
  34. #define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var)
  35. #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it)
  36. #define PB push_back
  37. #define PF push_front
  38. #define MP(a,b) make_pair(a,b)
  39. #define ST first
  40. #define ND second
  41. #define SIZE(x) (int)x.size()
  42.  
  43. const int N= 100;
  44. int tab_offset = 10;
  45. int row_offset = 100;
  46.  
  47. VS data;
  48. VS code;
  49.  
  50. int ile = 16;
  51. char txt[1000];
  52.  
  53. void init(int n) {
  54.   code.PB("MOVB 1, dot");
  55.   code.PB("MOVB 2, cross");
  56.   code.PB("MOVB 5, b10");
  57.   data.PB("dot:");
  58.   data.PB("db 46");
  59.   data.PB("cross:");
  60.   data.PB("db 88");
  61.   data.PB("b10:");
  62.   data.PB("db 10");
  63.  
  64.  
  65.   REP(mask, ile) {
  66.     REP(i,4) {
  67.       sprintf(txt, "MOVB %d, %d", tab_offset + mask * 4 + i, 1+(!!((1<<i) & mask)));
  68.       code.PB(txt);
  69.     }
  70.   }
  71.  
  72.   int i = 0;
  73.   while (i < n) {
  74.     sprintf(txt, "MOV %d, %d", row_offset + i, tab_offset + 0 * 4);
  75.     code.PB(txt);
  76.     i += 4;
  77.   }
  78. }
  79.  
  80. void add_row(VI v, VI previous) {
  81.   int n = SIZE(v);
  82.   int orign = n;
  83.   int i = 0;
  84.   while (n % 4) {
  85.     v.PB(0); previous.PB(0);
  86.     n++;
  87.   }
  88.  
  89.   while (i < n) {
  90.     int diff = 0;
  91.     REP(j, 4) if (v[i + j] != previous[i + j]) diff = 1;
  92.     if (diff) {
  93.       int mask = 0;
  94.       REP(j, 4) if (v[i + j]) mask += 1<<j;
  95.       sprintf(txt, "MOV %d, %d", row_offset + i, tab_offset + mask * 4);
  96.       code.PB(txt);
  97.     }
  98.     i += 4;
  99.   }
  100.  
  101.   static int nr = 0;
  102.   ++nr;
  103.  
  104.   sprintf(txt, "MOV 200, ret_label%d_pointer", nr);
  105.   code.PB(txt);
  106.  
  107.   sprintf(txt, "JMP out_function");
  108.   code.PB(txt);
  109.  
  110.   sprintf(txt, "ret_label%d:", nr);
  111.   code.PB(txt);
  112.  
  113.   sprintf(txt, "ret_label%d_pointer:", nr);
  114.   data.PB(txt);
  115.   sprintf(txt, "dw ret_label%d", nr);
  116.   data.PB(txt);
  117.  
  118. }
  119.  
  120. int main(){
  121.   int n = -1;
  122.   char txt[N];
  123.   VI previous;
  124.   int first = 1;
  125.   while (scanf("%s", txt) == 1) {
  126.     int k = strlen(txt);
  127.     if (n == -1) {
  128.       n = k;
  129.       previous = VI(n,0);
  130.     }
  131.     assert(k == n);
  132.     VI v2;
  133.     REP(i,n) v2.PB(txt[i] == 'X');
  134.  
  135.     if (first) {
  136.       init(n);
  137.       first = 0;
  138.     }
  139.     add_row(v2, previous);
  140.     previous = v2;
  141.   }
  142.   code.PB("JMP 0");
  143.  
  144.   int i = 0;
  145.   code.PB("out_function:");
  146.   while (i < n) {
  147.     if (i + 4 <= n) {
  148.       sprintf(txt, "OUT %d", row_offset + i);
  149.       i += 4;
  150.       code.PB(txt);
  151.     } else {
  152.       sprintf(txt, "OUTB %d", row_offset + i);
  153.       i++;
  154.       code.PB(txt);
  155.     }
  156.   }
  157.   code.PB("OUTB 5");
  158.   code.PB("JMP *200");
  159.  
  160.   for (auto s : code) printf("%s\n", s.c_str());
  161.   for (auto s : data) printf("%s\n", s.c_str());
  162.   return 0;
  163. }
RAW Paste Data
Want to get better at C++?
Learn to code C++ in 2017
Top