Advent of Code 2024 - Day 12

Greetings, Erlang enthusiasts! :wave: Ukrainian Erlanger here! :metal: Today, we’re diving into the art of fencing - literally. As the Elves struggle to calculate the fencing costs for the garden plots, we’re here to lend a hand with the power of Erlang!

The task: Determine the total price of fencing all regions on the garden map. Each region is defined by connected plots of the same type, and the price is calculated as the product of its area and perimeter.

:bulb: Tips for solving Day 12:

  • Flood-fill or DFS/BFS is great for identifying connected regions of the same type.
  • For each region, calculate its area by counting the number of plots and its perimeter by checking exposed edges.
  • Efficiently parse the map into a grid or coordinate system for easier traversal.

:sparkles: Challenge Motivation:
From connected plots to calculated costs, let’s fence it all and BEAM together!

Share your approaches, solutions, or any challenges you’re facing. Whether you’re building fences or debugging edge cases, this is the place to collaborate and learn.

Happy coding, and may your fences always be well-calculated! :christmas_tree::computer:

This one gave me a bit of trouble. Mainly because I tried to write a recursive algorithm to identify the groups and it is more brittle than I would like.

I came here to see if I could find an algorithm to better figure out the grouping. I’ll try the ones you mentioned.

My part 1: advent_of_code_2024/src/solution_day_12_2024.erl at main · chiroptical/advent_of_code_2024 · GitHub

I am definitely slowing down but plan to do a few more hopefully.

1 Like

IIUC, what I have is close. I just need to introduce a Visited map to avoid repeated visits.

1 Like

Great work on tackling it so far! Recursive algorithms can be tricky for grouping problems, especially without a Visited map - it’s a game-changer for avoiding repeated visits and infinite loops. Don’t worry about slowing down; this pace allows for reflection and learning. The solutions you’ll explore from here will only get more robust. Keep it up, and good luck refining your approach! :rocket::muscle: