-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.cs
136 lines (105 loc) · 4.32 KB
/
Day12.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using Aoc2024Net.Utilities;
namespace Aoc2024Net.Days
{
internal sealed class Day12 : Day
{
private record Edge(Coordinate From, Coordinate To);
private record Region(Coordinate[] Coordinates, Edge[] Edges);
private static readonly Coordinate[] Moves =
[
new Coordinate(1, 0),
new Coordinate(-1, 0),
new Coordinate(0, 1),
new Coordinate(0, -1),
];
public override object? SolvePart1() =>
GetRegions().Sum(r => r.Coordinates.Length * r.Edges.Length);
public override object? SolvePart2()
{
var regions = GetRegions();
var result = 0;
foreach (var region in regions)
{
var sortedEdges = region
.Edges
.OrderBy(e => e.From.Y)
.ThenBy(e => e.From.X)
.ToList();
var sidesCount = 0;
while (sortedEdges.Count > 0)
{
var edge = sortedEdges.First();
while (edge != null)
{
sortedEdges.Remove(edge);
var nextEdge = sortedEdges.FirstOrDefault(e =>
e.From == edge.To &&
(edge.From.X == edge.To.X ? e.From.X == e.To.X : e.From.Y == e.To.Y) &&
region.Edges.Count(ee => ee != edge && (e.From == ee.To || e.From == ee.From)) < 3);
edge = nextEdge;
}
sidesCount++;
}
result += region.Coordinates.Length * sidesCount;
}
return result;
}
private ICollection<Region> GetRegions() =>
GetRegions(GetRegionsCoordinates());
private ICollection<Coordinate[]> GetRegionsCoordinates()
{
var (grid, width, height) = InputData.GetInputCharGrid();
var visited = new HashSet<Coordinate>();
var regionsCoordinates = new List<Coordinate[]>();
foreach (var c in grid.GetAllCoordinates())
{
if (visited.Contains(c))
continue;
var plantType = grid.At(c);
var regionCoordinates = new List<Coordinate>();
var queue = new Queue<Coordinate>();
queue.Enqueue(c);
while (queue.Any())
{
var currentCoordinate = queue.Dequeue();
if (!visited.Add(currentCoordinate))
continue;
regionCoordinates.Add(currentCoordinate);
foreach (var move in Moves)
{
var nextCoordinate = currentCoordinate + move;
if (grid.IsInGrid(width, height, nextCoordinate) && grid.At(nextCoordinate) == plantType)
queue.Enqueue(nextCoordinate);
}
}
regionsCoordinates.Add(regionCoordinates.ToArray());
}
return regionsCoordinates;
}
private static ICollection<Region> GetRegions(ICollection<Coordinate[]> regionsCoordinates)
{
var regions = new List<Region>();
foreach (var regionCoordinates in regionsCoordinates)
{
var plotsEdges = new List<Edge>();
foreach (var c in regionCoordinates)
{
plotsEdges.AddRange(
[
new Edge(new Coordinate(c.X, c.Y), new Coordinate(c.X, c.Y + 1)),
new Edge(new Coordinate(c.X + 1, c.Y), new Coordinate(c.X + 1, c.Y + 1)),
new Edge(new Coordinate(c.X, c.Y), new Coordinate(c.X + 1, c.Y)),
new Edge(new Coordinate(c.X, c.Y + 1), new Coordinate(c.X + 1, c.Y + 1))
]);
}
var borderEdges = plotsEdges
.GroupBy(e => e)
.Where(g => g.Count() == 1)
.Select(g => g.First())
.ToArray();
regions.Add(new Region(regionCoordinates, borderEdges));
}
return regions;
}
}
}