Advertisement
Guest User

ISUCK

a guest
Dec 12th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. public class Day12
  2. {
  3.     private char[,] input;
  4.     private int cols;
  5.     private int rows;
  6.     private HashSet<(int x, int y)> workedCells;
  7.     private List<(int x, int y)> directions = [(0, -1), (1, 0), (0, 1), (-1, 0)];
  8.     public Day12()
  9.     {
  10.         Console.WriteLine("Day12");
  11.         input = GetInput(false);
  12.         cols = input.GetLength(0);
  13.         rows = input.GetLength(1);
  14.         Day12P1();
  15.         Day12P2();
  16.     }
  17.  
  18.     private void Day12P1()
  19.     {
  20.         workedCells = [];
  21.         int fenceCost = 0;
  22.         for (int y = 0; y < rows; y++)
  23.         {
  24.             for (int x = 0; x < cols; x++)
  25.             {
  26.                 if (!workedCells.Contains((x, y)))
  27.                 {
  28.                     fenceCost += BeginNewSection(x, y, input[x, y]);
  29.                 }
  30.             }
  31.         }
  32.         Console.WriteLine(fenceCost);
  33.     }
  34.  
  35.     private void Day12P2()
  36.     {
  37.         workedCells = [];
  38.         int fenceCost = 0;
  39.         for (int y = 0; y < rows; y++)
  40.         {
  41.             for (int x = 0; x < cols; x++)
  42.             {
  43.                 if (!workedCells.Contains((x, y)))
  44.                 {
  45.                     fenceCost += BeginNewSection(x, y, input[x, y], true);
  46.                 }
  47.             }
  48.         }
  49.         Console.WriteLine(fenceCost);
  50.     }
  51.  
  52.     private int BeginNewSection(int x, int y, char type, bool useCorners = false)
  53.     {
  54.         var plantPlot = WorkCell(x, y, type);
  55.         //Console.WriteLine($"{type} has {plantPlot.edges} fences and {plantPlot.cells} cells and {plantPlot.corners} corners");
  56.         if (useCorners) return plantPlot.corners * plantPlot.cells;
  57.         return plantPlot.edges * plantPlot.cells;
  58.     }
  59.  
  60.     private (int edges, int cells, int corners) WorkCell(int x, int y, char type)
  61.     {
  62.         if (workedCells.Contains((x, y)) || x < 0 || x >= cols || y < 0 || y >= rows) return (0, 0, 0);
  63.         workedCells.Add((x, y));
  64.         int edges = 0;
  65.         int cells = 1;
  66.         int corners = 0;
  67.         for (int i = 0; i < directions.Count; i++)
  68.         {
  69.             var dir = directions[i];
  70.             (int x, int y) cellToCheck = (x + dir.x, y + dir.y);
  71.             bool cellIsOutOfBounds = false;
  72.             if (cellToCheck.x < 0 || cellToCheck.x >= cols || cellToCheck.y < 0 || cellToCheck.y >= rows) cellIsOutOfBounds = true;
  73.             if (cellIsOutOfBounds || input[cellToCheck.x, cellToCheck.y] != type) edges++;
  74.             if (!cellIsOutOfBounds && !workedCells.Contains(cellToCheck) && input[cellToCheck.x, cellToCheck.y] == type)
  75.             {
  76.                 var workedCell = WorkCell(cellToCheck.x, cellToCheck.y, type);
  77.                 edges += workedCell.edges;
  78.                 cells += workedCell.cells;
  79.                 corners += workedCell.corners;
  80.             }
  81.             if (cellIsOutOfBounds || input[cellToCheck.x, cellToCheck.y] != type)
  82.             {
  83.                 (int x, int y) cornerIndexToCheck = (directions[(i + 1) % 4].x + x, directions[(i + 1) % 4].y + y);
  84.                 bool nextCellIsOutOfBounds = false;
  85.                 if (cornerIndexToCheck.x < 0 || cornerIndexToCheck.x >= cols || cornerIndexToCheck.y < 0 || cornerIndexToCheck.y >= rows) nextCellIsOutOfBounds = true;
  86.                 if (nextCellIsOutOfBounds || input[cornerIndexToCheck.x, cornerIndexToCheck.y] != type) corners++;
  87.                 else
  88.                 {
  89.                     (int x, int y) innerCornerCheck = (directions[(i + 1) % 4].x + cellToCheck.x, directions[(i + 1) % 4].y + cellToCheck.y);
  90.                     bool innerCornerCheckIsOutOfBounds = false;
  91.                     if (innerCornerCheck.x < 0 || innerCornerCheck.x >= cols || innerCornerCheck.y < 0 || innerCornerCheck.y >= rows) innerCornerCheckIsOutOfBounds = true;
  92.                     if (!innerCornerCheckIsOutOfBounds && input[innerCornerCheck.x, innerCornerCheck.y] == type) corners++;
  93.                 }
  94.             }
  95.         }
  96.         return (edges, cells, corners);
  97.     }
  98.  
  99.     private char[,] GetInput(bool test)
  100.     {
  101.         string inputRaw;
  102.         if (test)
  103.         {
  104.             inputRaw =
  105. """
  106. RRRRIICCFF
  107. RRRRIICCCF
  108. VVRRRCCFFF
  109. VVRCCCJFFF
  110. VVVVCJJCFE
  111. VVIVCCJJEE
  112. VVIIICJJEE
  113. MIIIIIJJEE
  114. MIIISIJEEE
  115. MMMISSJEEE
  116. """; //p1 1930 p2 1206
  117.      //             inputRaw =
  118.      // """
  119.      // AAAAAA
  120.      // AAABBA
  121.      // AAABBA
  122.      // ABBAAA
  123.      // ABBAAA
  124.      // AAAAAA
  125.      // """; //p2 368
  126.         }
  127.         else
  128.         {
  129.             inputRaw = File.ReadAllText(@"Data\Day12.txt");
  130.         }
  131.         var c = inputRaw.Split("\n")[0].Trim().Length;
  132.         var r = inputRaw.Split('\n').Length;
  133.         char[,] input = new char[c, r];
  134.         for (int y = 0; y < r; y++)
  135.         {
  136.             var row = inputRaw.Split("\n")[y].Trim();
  137.             for (int x = 0; x < c; x++)
  138.             {
  139.                 input[x, y] = row[x];
  140.             }
  141.         }
  142.         return input;
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement