Advertisement
Equd

Advent Of Code Day 19 - 2017

Dec 19th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. public class Diagram
  2.     {
  3.         char[][] diag;
  4.         List<char> lstChars = new List<char>();
  5.  
  6.         public void Solve()
  7.         {
  8.             diag = Properties.Resources.TextFile1.Split('\n').Select(f => f.ToArray()).ToArray();
  9.  
  10.             int x = new string(diag[0]).IndexOf('|');
  11.             int y = 0;
  12.  
  13.             try
  14.             {
  15.                 MoveDown(x, y);
  16.             }
  17.             catch
  18.             {
  19.  
  20.             }
  21.  
  22.             var answerA = new string(lstChars.Where(q => Char.IsLetter(q)).ToArray());
  23.             var answerB = lstChars.Count();
  24.         }
  25.  
  26.         public bool MoveDown(int x, int y)
  27.         {
  28.             if (diag[y + 1][x] == ' ')
  29.                 return false;
  30.  
  31.             lstChars.Add(diag[y][x]);
  32.             while (diag[++y][x] != '+')
  33.             {
  34.                 if (diag[y][x] == ' ') return true;
  35.                 lstChars.Add(diag[y][x]);
  36.             }
  37.  
  38.  
  39.             return (MoveLeft(x, y) || MoveRigh(x, y));            
  40.         }
  41.  
  42.         private bool MoveRigh(int x, int y)
  43.         {
  44.             if (diag[y][x + 1] == ' ') return false;
  45.  
  46.             lstChars.Add(diag[y][x]);
  47.             while (diag[y][++x] != '+')
  48.             {
  49.                 if (diag[y][x] == ' ') return true;
  50.                 lstChars.Add(diag[y][x]);                
  51.             }
  52.  
  53.            return (MoveUp(x, y) || MoveDown(x, y));            
  54.         }
  55.  
  56.         private bool MoveLeft(int x, int y)
  57.         {
  58.             if (diag[y][x - 1] == ' ') return false;
  59.  
  60.             lstChars.Add(diag[y][x]);
  61.             while (diag[y][--x] != '+')
  62.             {
  63.                 if (diag[y][x] == ' ') return true;
  64.                 lstChars.Add(diag[y][x]);
  65.             }
  66.  
  67.             return (MoveUp(x, y) || MoveDown(x, y));
  68.         }
  69.  
  70.         private bool MoveUp(int x, int y)
  71.         {
  72.             if (diag[y - 1][x] == ' ') return false;
  73.  
  74.             lstChars.Add(diag[y][x]);
  75.             while (diag[--y][x] != '+')
  76.             {
  77.                 if (diag[y][x] == ' ') return true;
  78.                 lstChars.Add(diag[y][x]);
  79.             }
  80.  
  81.             return (MoveLeft(x, y) || MoveRigh(x, y));
  82.            
  83.         }
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement