Advertisement
Guest User

AoC #7 (Welltype)

a guest
Dec 7th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. impure function part1() : uint
  2. {
  3.     uint num;
  4.  
  5.     file f = fopen("input", 'r');
  6.     string ln;
  7.  
  8.     for(ln=freadln(f);!empty(ln);ln=freadln(f))
  9.     {
  10.         bool ok, abba, bracket = true, false, false;
  11.         char t1, t2, t3, t4 = '\x01', '\x02', '\x03', '\x04';
  12.  
  13.         foreach(ch in ln while ok)
  14.         {
  15.             t1, t2, t3, t4 = t2, t3, t4, ch;
  16.  
  17.             if('['==ch)
  18.             {
  19.                 bracket = true;
  20.             }
  21.             elif(']'==ch)
  22.             {
  23.                 bracket = false;
  24.             }
  25.             elif(t1!=t2 &&& t1==t4 &&& t2==t3)
  26.             {
  27.                 if(bracket) { ok   = false; }
  28.                 else        { abba = true;  }
  29.             }
  30.         }
  31.  
  32.         if(ok &&& abba)
  33.         {
  34.             num = num + 1u;
  35.         }
  36.     }
  37.  
  38.     return num;
  39. }
  40.  
  41. declare
  42. {
  43.     record Pair
  44.     {
  45.         char first;
  46.         char second;
  47.     }
  48. }
  49.  
  50. function has(seq<Pair> ps, Pair p) : bool
  51. {
  52.     foreach(x in ps)
  53.     {
  54.         if(x.first==p.first &&& x.second==p.second)
  55.         {
  56.             return true;
  57.         }
  58.     }
  59.  
  60.     return false;
  61. }
  62.  
  63. impure function part2() : uint
  64. {
  65.     uint num;
  66.  
  67.     file f = fopen("input", 'r');
  68.     string ln;
  69.  
  70.     for(ln=freadln(f);!empty(ln);ln=freadln(f))
  71.     {
  72.         seq<Pair> aba, bab;
  73.         bool bracket = false;
  74.         char t1, t2, t3 = '\x01', '\x02', '\x03';
  75.  
  76.         foreach(ch in ln)
  77.         {
  78.             t1, t2, t3 = t2, t3, ch;
  79.  
  80.             if('['==ch)
  81.             {
  82.                 bracket = true;
  83.             }
  84.             elif(']'==ch)
  85.             {
  86.                 bracket = false;
  87.             }
  88.             if(t1!=t2 &&& t1==t3)
  89.             {
  90.                 if(bracket)
  91.                 {
  92.                     Pair p = Pair { t2, t1 };
  93.                     if(!has(bab, p)) { bab = hiext(bab, p); }
  94.                 }
  95.                 else
  96.                 {
  97.                     Pair p = Pair { t1, t2 };
  98.                     if(!has(aba, p)) { aba = hiext(aba, p); }
  99.                 }
  100.             }
  101.         }
  102.  
  103.         bool found = false;
  104.  
  105.         foreach(a in aba while !found)
  106.         {
  107.             found = has(bab, a);
  108.         }
  109.  
  110.         if(found)
  111.         {
  112.             num = num + 1u;
  113.         }
  114.     }
  115.  
  116.     return num;
  117. }
  118.  
  119. main
  120. {
  121.     write(tos(part1()) + "\n");
  122.     write(tos(part2()) + "\n");
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement