Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Day12
- {
- private char[,] input;
- private int cols;
- private int rows;
- private HashSet<(int x, int y)> workedCells;
- private List<(int x, int y)> directions = [(0, -1), (1, 0), (0, 1), (-1, 0)];
- public Day12()
- {
- Console.WriteLine("Day12");
- input = GetInput(false);
- cols = input.GetLength(0);
- rows = input.GetLength(1);
- Day12P1();
- Day12P2();
- }
- private void Day12P1()
- {
- workedCells = [];
- int fenceCost = 0;
- for (int y = 0; y < rows; y++)
- {
- for (int x = 0; x < cols; x++)
- {
- if (!workedCells.Contains((x, y)))
- {
- fenceCost += BeginNewSection(x, y, input[x, y]);
- }
- }
- }
- Console.WriteLine(fenceCost);
- }
- private void Day12P2()
- {
- workedCells = [];
- int fenceCost = 0;
- for (int y = 0; y < rows; y++)
- {
- for (int x = 0; x < cols; x++)
- {
- if (!workedCells.Contains((x, y)))
- {
- fenceCost += BeginNewSection(x, y, input[x, y], true);
- }
- }
- }
- Console.WriteLine(fenceCost);
- }
- private int BeginNewSection(int x, int y, char type, bool useCorners = false)
- {
- var plantPlot = WorkCell(x, y, type);
- //Console.WriteLine($"{type} has {plantPlot.edges} fences and {plantPlot.cells} cells and {plantPlot.corners} corners");
- if (useCorners) return plantPlot.corners * plantPlot.cells;
- return plantPlot.edges * plantPlot.cells;
- }
- private (int edges, int cells, int corners) WorkCell(int x, int y, char type)
- {
- if (workedCells.Contains((x, y)) || x < 0 || x >= cols || y < 0 || y >= rows) return (0, 0, 0);
- workedCells.Add((x, y));
- int edges = 0;
- int cells = 1;
- int corners = 0;
- for (int i = 0; i < directions.Count; i++)
- {
- var dir = directions[i];
- (int x, int y) cellToCheck = (x + dir.x, y + dir.y);
- bool cellIsOutOfBounds = false;
- if (cellToCheck.x < 0 || cellToCheck.x >= cols || cellToCheck.y < 0 || cellToCheck.y >= rows) cellIsOutOfBounds = true;
- if (cellIsOutOfBounds || input[cellToCheck.x, cellToCheck.y] != type) edges++;
- if (!cellIsOutOfBounds && !workedCells.Contains(cellToCheck) && input[cellToCheck.x, cellToCheck.y] == type)
- {
- var workedCell = WorkCell(cellToCheck.x, cellToCheck.y, type);
- edges += workedCell.edges;
- cells += workedCell.cells;
- corners += workedCell.corners;
- }
- if (cellIsOutOfBounds || input[cellToCheck.x, cellToCheck.y] != type)
- {
- (int x, int y) cornerIndexToCheck = (directions[(i + 1) % 4].x + x, directions[(i + 1) % 4].y + y);
- bool nextCellIsOutOfBounds = false;
- if (cornerIndexToCheck.x < 0 || cornerIndexToCheck.x >= cols || cornerIndexToCheck.y < 0 || cornerIndexToCheck.y >= rows) nextCellIsOutOfBounds = true;
- if (nextCellIsOutOfBounds || input[cornerIndexToCheck.x, cornerIndexToCheck.y] != type) corners++;
- else
- {
- (int x, int y) innerCornerCheck = (directions[(i + 1) % 4].x + cellToCheck.x, directions[(i + 1) % 4].y + cellToCheck.y);
- bool innerCornerCheckIsOutOfBounds = false;
- if (innerCornerCheck.x < 0 || innerCornerCheck.x >= cols || innerCornerCheck.y < 0 || innerCornerCheck.y >= rows) innerCornerCheckIsOutOfBounds = true;
- if (!innerCornerCheckIsOutOfBounds && input[innerCornerCheck.x, innerCornerCheck.y] == type) corners++;
- }
- }
- }
- return (edges, cells, corners);
- }
- private char[,] GetInput(bool test)
- {
- string inputRaw;
- if (test)
- {
- inputRaw =
- """
- RRRRIICCFF
- RRRRIICCCF
- VVRRRCCFFF
- VVRCCCJFFF
- VVVVCJJCFE
- VVIVCCJJEE
- VVIIICJJEE
- MIIIIIJJEE
- MIIISIJEEE
- MMMISSJEEE
- """; //p1 1930 p2 1206
- // inputRaw =
- // """
- // AAAAAA
- // AAABBA
- // AAABBA
- // ABBAAA
- // ABBAAA
- // AAAAAA
- // """; //p2 368
- }
- else
- {
- inputRaw = File.ReadAllText(@"Data\Day12.txt");
- }
- var c = inputRaw.Split("\n")[0].Trim().Length;
- var r = inputRaw.Split('\n').Length;
- char[,] input = new char[c, r];
- for (int y = 0; y < r; y++)
- {
- var row = inputRaw.Split("\n")[y].Trim();
- for (int x = 0; x < c; x++)
- {
- input[x, y] = row[x];
- }
- }
- return input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement