Day 12: Garden Groups

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

You are viewing a single thread.
View all comments
2 points

Ended up oversleeping somewhat, so I did the first part on the way to work using flood fills over a global visited set, and now that work’s over I’ve sat down to expand that solution to do corner counting for part two as well.

C#
char[] map = new char[0];
(int X, int Y) size = (0, 0);

public void Input(IEnumerable<string> lines)
{
  map = string.Concat(lines).ToCharArray();
  size = (lines.First().Length, lines.Count());
}

Dictionary<HashSet<(int,int)>,int> areas = new Dictionary<HashSet<(int,int)>,int>();
public void PreCalc()
{
  HashSet<(int, int)> visited = new HashSet<(int, int)>();
  for (int y = 0; y < size.Y; ++y)
    for (int x = 0; x < size.X; ++x)
    {
      var at = (x, y);
      if (visited.Contains(at))
        continue;

      var area = Flood((x, y), visited);
      areas[area.Area] = area.Perim;
    }
}

public void Part1()
{
  int sum = areas.Select(kv => kv.Key.Count * kv.Value).Sum();

  Console.WriteLine($"Fencing total: {sum}");
}
public void Part2()
{
  int sum = areas.Select(kv => kv.Key.Count * countCorners(kv.Key)).Sum();

  Console.WriteLine($"Fencing total: {sum}");
}

readonly (int dX, int dY)[] links = new[] { (1, 0), (0, 1), (-1, 0), (0, -1) };
(HashSet<(int,int)> Area, int Perim) Flood((int X, int Y) from, HashSet<(int X, int Y)> visited)
{
  char at = map[from.Y * size.X + from.X];

  (HashSet<(int,int)> Area, int Perim) ret = (new HashSet<(int,int)>(), 0);
  visited.Add(from);
  ret.Area.Add(from);

  foreach (var link in links)
  {
    (int X, int Y) newAt = (from.X + link.dX, from.Y + link.dY);
    char offset ;
    if (newAt.X < 0 || newAt.X >= size.X || newAt.Y < 0 || newAt.Y >= size.Y)
      offset = '\0';
    else
      offset = map[newAt.Y * size.X + newAt.X];

    if (offset == at)
    {
      if (visited.Contains(newAt))
        continue;

      var nextArea = Flood(newAt, visited);
      ret.Area.UnionWith(nextArea.Area);
      ret.Perim += nextArea.Perim;
    }
    else
    {
      ret.Perim += 1;
    }
  }

  return ret;
}

readonly (int dX, int dY)[] cornerPoints = new[] { (0, 0), (1, 0), (1, 1), (0, 1) };
readonly int[] diagonalValues = new[] { (2 << 0) + (2 << 2), (2 << 1) + (2 << 3) };
int countCorners(HashSet<(int X, int Y)> points)
{
  int corners = 0;
  var bounds = findBounds(points);
  for (int y = bounds.minY - 1; y < bounds.maxY + 1; ++y)
    for (int x = bounds.minX - 1; x < bounds.maxX + 1; ++x)
    {
      var atPoint = cornerPoints.Select(c => points.Contains((x + c.dX, y + c.dY)));
      var before = corners;
      if (atPoint.Where(c => c).Count() % 2 == 1)
        corners++;
      else if (diagonalValues.Contains(atPoint.Select((c, i) => c ? (2 << i) : 0).Sum()))
        corners += 2;
    }

  return corners;
}

(int minX, int minY, int maxX, int maxY) findBounds(HashSet<(int X, int Y)> points)
{
  (int minX, int minY, int maxX, int maxY) ret = (int.MaxValue, int.MaxValue, int.MinValue, int.MinValue);
  foreach (var point in points)
  {
    ret.minX = Math.Min(ret.minX, point.X);
    ret.minY = Math.Min(ret.minY, point.Y);
    ret.maxX = Math.Max(ret.maxX, point.X);
    ret.maxY = Math.Max(ret.maxY, point.Y);
  }

  return ret;
}
permalink
report
reply

Advent Of Code

!advent_of_code@programming.dev

Create post

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
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

Rules/Guidelines

  • Follow the programming.dev instance rules
  • Keep all content related to advent of code in some way
  • If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
  • When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

Community stats

  • 467

    Monthly active users

  • 111

    Posts

  • 1.1K

    Comments