Equd

AdventofCode 2018 Day 03

Dec 3rd, 2018
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. int[,] canvas = null;
  2.  
  3. void Main()
  4. {
  5.     var aoc = new AdventOfCode(2018, 3);
  6.  
  7.     var input = new string[]{ "#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2", }.Select(x=> new Paper(x)).ToArray();
  8.  
  9.     //test if all is working
  10.     Debug.Assert(FillCanvas(10, input) == 4);
  11.     Debug.Assert(FindSoloID(input) == 3);
  12.    
  13.     //get results
  14.     input = aoc.InputLines.Select(x=> new Paper(x)).ToArray();
  15.  
  16.     //do your thing
  17.     aoc.SubmitAnswer(FillCanvas(1000, input), Part.A);
  18.     aoc.SubmitAnswer(FindSoloID(input), Part.B);
  19.  
  20.    
  21.  
  22. }
  23. //class for each Paper part
  24. class Paper
  25. {
  26.     public int ID; 
  27.     public int xStart;
  28.     public int yStart;
  29.     public int width;
  30.     public int height;
  31.        
  32.     Regex regex = new Regex(@"#(\d+) @ (\d+),(\d+): (\d+)x(\d+)"); 
  33.     public Paper(string line)
  34.     {              
  35.         var match = regex.Match(line);
  36.  
  37.         this.ID = int.Parse(match.Groups[1].Value);
  38.         this.xStart = int.Parse(match.Groups[2].Value);
  39.         this.yStart = int.Parse(match.Groups[3].Value);
  40.         this.width = int.Parse(match.Groups[4].Value);
  41.         this.height = int.Parse(match.Groups[5].Value);
  42.  
  43.     }
  44.  
  45. }
  46.  
  47. int FillCanvas(int size, Paper[] papers)
  48. {
  49.     //create canvas
  50.     canvas = new int[size,size];
  51.    
  52.     //check each paper
  53.     foreach (var paper in papers)
  54.     {      
  55.         //draw each paper
  56.         for (int x = paper.xStart; x < paper.xStart + paper.width; x++)
  57.         {
  58.             for (int y = paper.yStart; y < paper.yStart + paper.height; y++)
  59.             {
  60.                 //increment for how many papers are on this position
  61.                 canvas[x, y]++;
  62.             }
  63.         }
  64.     }
  65.    
  66.     //flatten and count >= 2
  67.     return canvas.Cast<int>().Where(x=> x >= 2).Count();
  68. }
  69.  
  70.  
  71. int FindSoloID(Paper[] papers)
  72. {
  73.     //check all papers
  74.     foreach (var paper in papers)
  75.     {
  76.         for (int x = paper.xStart; x < paper.xStart + paper.width; x++)
  77.         {
  78.             for (int y = paper.yStart; y < paper.yStart + paper.height; y++)
  79.             {
  80.                 //this paper has more than 1, so not it
  81.                 if (canvas[x, y] != 1) goto FAILED;
  82.  
  83.             }
  84.         }
  85.        
  86.         //this one has all
  87.         return paper.ID;
  88.     FAILED:
  89.         continue;
  90.     }
  91.     throw new ObjectNotFoundException();
  92. }
  93. // Define other methods and classes here
Add Comment
Please, Sign In to add comment