<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="349766" data-post-id="349766">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jarlah" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/120/24789_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jarlah
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>thanks <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"> corner counting is good now <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> Ill look at your example, but now im trying to find the non math way to do it. I might just go back to the drawing board <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> Or, just skip it. I have learned enough <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> btw done thanks for tolerating my grave digging <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"> i found out btw that the alternative approach was way more complex and didnt work the same on small inputs vs complex inputs. So i dropped digging more into it (i always begins with the simplest example and get it to work for all inputs, so when i suddenly get discrepancy for a larger input, I havent solved it and scraps it)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349766" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-349766" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349766"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #31"></div>
  </section>
</div>
    <div class="postbit" id="349780" data-post-id="349780">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bjorng" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/120/13187_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bjorng
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="jarlah" data-post="28" data-topic="68054">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/48/24789_2.png" class="avatar"> jarlah:</div>
<blockquote>
<p>Or maybe its hiding the same convex/concave in the code ?</p>
</blockquote>
</aside>
<p>No, nothing’s hiding here. <img src="https://forum.elixirforum.com/images/emoji/apple/smile.png?v=15" title=":smile:" class="emoji" alt=":smile:" loading="lazy" width="20" height="20"></p>
<p>My code connects all adjacent pieces of the fence. <code>diff</code> represents on which side of the plot the fence is located (above, below, left, or right). If the fence is above or below I want to group this plot with all plots on the same row, otherwise I want to group plots in the same column.</p>
<p>For example, take the e-shaped region in the puzzle descripton. <code>Enum.group_by/3</code> will produce the following groups for the region of E plots:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%{
 {{-1, 0}, 0} =&gt; [1, 2, 3, 4],     # The fence below row 0, columns 1..4
  {{-1, 0}, 2} =&gt; [1, 2, 3, 4],    # The fence below row 2, columns 1..4
  {{-1, 0}, 4} =&gt; [0, 1, 2, 3, 4], # The fence below row 4
  {{0, -1}, 0} =&gt; [1, 3],          # The fences right of column 0, rows 1 and 3
  {{0, -1}, 4} =&gt; [0, 2, 4],       # The fences right of column 4, rows, 0, 2, 4
  {{0, 1}, 0} =&gt; [0, 1, 2, 3, 4],
  {{1, 0}, 0} =&gt; [0, 1, 2, 3, 4],
  {{1, 0}, 2} =&gt; [1, 2, 3, 4],
  {{1, 0}, 4} =&gt; [1, 2, 3, 4]
}
</code></pre>
<p>The lists with non-consecutive numbers (<code>[1, 3]</code> and <code>[0, 2, 4]</code>) need to be split as each such list represents more than one side. After splitting we have the following lists:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [0, 1, 2, 3, 4],
  [1],
  [3],
  [0],
  [2],
  [4],
  [0, 1, 2, 3, 4],
  [0, 1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
]
</code></pre>
<p>There are now 12 sublists, where each sublist represents a side in the fence. By counting the number sublists, we get the number of sides.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349780" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-349780" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349780"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #32"></div>
  </section>
</div>
    <div class="postbit" id="349782" data-post-id="349782">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jarlah" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/120/24789_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jarlah
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>i think the chunk_while threw me off the bus maybe … other than that your explanation makes perfect sense when you say it like this <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349782" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-349782" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349782"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #33"></div>
  </section>
</div>
    <div class="postbit" id="349786" data-post-id="349786">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lud" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/120/14382_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lud
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution is not math, it’s exactly the same as Bjorn’s <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349786" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-349786" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349786"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #34"></div>
  </section>
</div>
    <div class="postbit" id="349792" data-post-id="349792">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jarlah" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/120/24789_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jarlah
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>looked at it. I think i need to look more closely at it. But looks nicer. No wierd chunk while <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>Unrelated segway:<br>
i have been recently thinking about making an umbrella project in mix with a shared common project and sub projects for all years, and sub sub projects for all days .. then they are sort of standalone .. and if one day cant use the standard tooling or some depenency … i can just break it loose from the umbrella for that specific day</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349792" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-349792" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349792"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #35"></div>
  </section>
</div>
    <div class="postbit" id="349794" data-post-id="349794">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jarlah" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/120/24789_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jarlah
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>ey .. what .. i can actually use normal function overloading in elixir ??</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp distinct_sides(cross_coords) do
    [h | cross_coords] = Enum.sort(cross_coords)
    distinct_sides(cross_coords, h, 0)
  end

  defp distinct_sides([h | t], prev, acc) when h == prev + 1 do
</code></pre>
<p>this got me spinning ! learned something new! yay no more new names for  functions just because i need to do recursion. Makes sense though distinct_sides/1 and distinct_sides/3 ARE in fact different. Cognitive overhead though .. <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>but seriously i am actually understanding whats going on with your code.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349794" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-349794" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349794"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #36"></div>
  </section>
</div>
    <div class="postbit" id="349801" data-post-id="349801">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="lud" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/120/14382_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  lud
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="jarlah" data-post="37" data-topic="68054">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/48/24789_2.png" class="avatar"> jarlah:</div>
<blockquote>
<p>Makes sense though distinct_sides/1 and distinct_sides/3 ARE in fact different.</p>
</blockquote>
</aside>
<p>Yes despite the exact same name, because of the different arity, those functions in Elixir and Erlang are totally different functions that can have their own <a class="mention" href="/u/spec" rel="nofollow">@spec</a>, <a class="mention" href="/u/doc" rel="nofollow">@doc</a>, one could be exported while the other remains private, etc.</p>
<blockquote>
<p>i have been recently thinking about making an umbrella project in mix with a shared common project and sub projects for all years, and sub sub projects for all days</p>
</blockquote>
<p>You do you but honestly this looks like pain <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"><br>
I really like to be able to change some stuff in my shared modules and just call <code>mix test</code> to run all solutions for all years i’ve done without thinking about project structure.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349801" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-349801" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349801"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #37"></div>
  </section>
</div>
    <div class="postbit" id="349808" data-post-id="349808">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="cblavier" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cblavier/120/29763_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  cblavier
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I struggled so much on the past few days that I’m proud this one went smoothly.</p>
<p>I confess that I read before coding anything that counting corners was a good strategy <img src="https://forum.elixirforum.com/images/emoji/apple/bulb.png?v=15" title=":bulb:" class="emoji" alt=":bulb:" loading="lazy" width="20" height="20"></p>
<p><strong>Part 1:</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2024.Day12.Part1 do
  defmodule Region do
    defstruct [:plant, :area, :perimeter, :sides, plots: MapSet.new()]
  end

  def run(puzzle) do
    puzzle
    |&gt; parse()
    |&gt; find_regions()
    |&gt; Enum.map(&amp;compute_area_and_perimeter/1)
    |&gt; Enum.map(&amp;(&amp;1.area * &amp;1.perimeter))
    |&gt; Enum.sum()
  end

  def parse(puzzle) do
    for {row, y} &lt;- puzzle |&gt; String.split("\n") |&gt; Enum.with_index(),
        {plant, x} &lt;- row |&gt; String.graphemes() |&gt; Enum.with_index(),
        into: %{},
        do: {{x, y}, plant}
  end

  def find_regions(farm) do
    Enum.reduce(farm, {[], MapSet.new()}, fn {{x, y}, plant}, {regions, visited} -&gt;
      if MapSet.member?(visited, {x, y}) do
        {regions, visited}
      else
        region = explore(farm, {x, y}, %Region{plant: plant})
        {[region | regions], MapSet.union(visited, MapSet.new(region.plots))}
      end
    end)
    |&gt; elem(0)
  end

  def explore(farm, {x, y}, region = %Region{plant: plant, plots: plots}) do
    region = %Region{region | plots: MapSet.put(plots, {x, y})}

    for {nx, ny} &lt;- [{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}], reduce: region do
      region -&gt;
        if Map.get(farm, {nx, ny}) == plant and not MapSet.member?(plots, {nx, ny}) do
          explore(farm, {nx, ny}, region)
        else
          region
        end
    end
  end

  defp compute_area_and_perimeter(region = %Region{plots: plots}) do
    fences =
      for {x, y} &lt;- plots,
          {nx, ny} &lt;- [{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}],
          not MapSet.member?(plots, {nx, ny}),
          do: true

    %Region{region | area: MapSet.size(plots), perimeter: length(fences)}
  end
end
</code></pre>
<p><strong>Part 2:</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2024.Day12.Part2 do
  alias Advent.Y2024.Day12.Part1
  alias Advent.Y2024.Day12.Part1.Region

  import MapSet, only: [member?: 2]

  def run(puzzle) do
    puzzle
    |&gt; Part1.parse()
    |&gt; Part1.find_regions()
    |&gt; Enum.map(&amp;compute_area_and_sides/1)
    |&gt; Enum.map(&amp;(&amp;1.area * &amp;1.sides))
    |&gt; Enum.sum()
  end

  defp compute_area_and_sides(region = %Region{plots: plots}) do
    corners =
      for {x, y} &lt;- plots,
          [{d1x, d1y}, {d2x, d2y}, {d3x, d3y}] &lt;- [
            [{-1, 0}, {-1, -1}, {0, -1}],
            [{0, -1}, {1, -1}, {1, 0}],
            [{1, 0}, {1, 1}, {0, 1}],
            [{0, 1}, {-1, 1}, {-1, 0}]
          ],
          [n1, n2, n3] = [{x + d1x, y + d1y}, {x + d2x, y + d2y}, {x + d3x, y + d3y}],
          (not member?(plots, n1) and not member?(plots, n3)) or
            (member?(plots, n1) and not member?(plots, n2) and member?(plots, n3)),
          do: true

    %Region{region | area: MapSet.size(plots), sides: length(corners)}
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349808" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-349808" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349808"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #38"></div>
  </section>
</div>
    <div class="postbit" id="349854" data-post-id="349854">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jarlah" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jarlah/120/24789_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jarlah
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>made advancement to my setup</p>
<p><a href="https://github.com/jarlah/advent_of_code" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code</a></p>
<p>first i removed all input files and AOC descriptions and washed my git commit log.</p>
<p>then i moved every year into solutions directory and changed my mix task to generate actual mix projects that depend on a common mix project two levels above it</p>
<p>so now i have shared code in a common project with no umbrella <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> and the parent project wont compile the code for the other projects since its not in the lib folder, it will only compile the mix task</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349854" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-349854" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349854"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #39"></div>
  </section>
</div>
    <div class="postbit" id="349880" data-post-id="349880">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ken-kost" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ken-kost/120/40209_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ken-kost
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Man I got stuck on this one. First I wasn’t able to parse correctly i.e. get the groups. Tried multiple approaches and although they would work on test examples it wouldn’t on the input. Day after I cracked it but then got stuck on part 2. So I was bashing my head yet again. Went here to maybe get an idea, and I did thanks to <a class="mention" href="/u/bjorng" rel="nofollow">@bjorng</a></p>
<blockquote>
<p>My code connects all adjacent pieces of the fence.</p>
</blockquote>
<p>So that’s what I tried as well. I unpacked the fence calculation from part 1 to preserve sides information and then parsed that. Now I’m behind 3 days and it’s only getting harder. <img src="https://forum.elixirforum.com/images/emoji/apple/tired_face.png?v=15" title=":tired_face:" class="emoji" alt=":tired_face:" loading="lazy" width="20" height="20"> But I’ll persevere. <img src="https://forum.elixirforum.com/images/emoji/apple/triumph.png?v=15" title=":triumph:" class="emoji" alt=":triumph:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day12 do
  alias AoC.Input

  def parse(input, _part) do
    [{first_line, 0} | rest] =
      input
      |&gt; Input.stream!(trim: true)
      |&gt; Enum.with_index()

    first_chunks =
      first_line
      |&gt; String.codepoints()
      |&gt; Enum.with_index()
      |&gt; Enum.map(fn {e, y} -&gt; {e, 0, y} end)
      |&gt; Enum.chunk_by(fn {e, _, _} -&gt; e end)

    Enum.reduce(rest, first_chunks, fn {line, x}, acc -&gt;
      line
      |&gt; String.codepoints()
      |&gt; Enum.with_index()
      |&gt; Enum.map(fn {e, y} -&gt; {e, x, y} end)
      |&gt; Enum.chunk_by(fn {e, _, _} -&gt; e end)
      |&gt; Enum.reduce(acc, fn chunk, groups -&gt;
        group_chunk(groups, chunk)
      end)
    end)
  end

  def part_one(problem) do
    problem
    |&gt; Enum.reduce(0, fn group, acc -&gt;
      group = Enum.map(group, fn {_, x, y} -&gt; {x, y} end)

      fence =
        Enum.reduce(group, 0, fn point, acc -&gt;
          acc + length(calculate_fence(point, group))
        end)

      acc + length(group) * fence
    end)
  end

  def part_two(problem) do
    problem
    |&gt; Enum.map(fn group -&gt;
      group = Enum.map(group, fn {_, x, y} -&gt; {x, y} end)

      Enum.reduce(group, [], fn point, acc -&gt;
        [{point, calculate_fence(point, group)} | acc]
      end)
    end)
    |&gt; Enum.map(fn group -&gt;
      h_chunks = Enum.chunk_by(group, fn {{x, _y}, _} -&gt; x end)
      counter_h = count_sides(h_chunks, :up, 1) + count_sides(h_chunks, :down, 1)
      v_chunks = Enum.group_by(group, fn {{_x, y}, _} -&gt; y end) |&gt; Map.values()
      counter_v = count_sides(v_chunks, :left, 0) + count_sides(v_chunks, :right, 0)
      {length(group), counter_h + counter_v}
    end)
    |&gt; Enum.reduce(0, fn {length, sides}, acc -&gt; acc + length * sides end)
  end

  defp group_chunk(groups, chunk) do
    case Enum.split_with(groups, fn group -&gt; belongs_to?(group, chunk) end) do
      {[], groups} -&gt; [chunk | groups]
      {chunk_groups, groups} -&gt; [Enum.concat(chunk_groups ++ [chunk]) | groups]
    end
  end

  defp belongs_to?(group, chunk) do
    Enum.any?(group, fn {e, x, y} -&gt; {e, x + 1, y} in chunk end)
  end

  defp calculate_fence({x, y}, list) do
    result = []
    result = if {x + 1, y} in list, do: result, else: [:down | result]
    result = if {x - 1, y} in list, do: result, else: [:up | result]
    result = if {x, y + 1} in list, do: result, else: [:right | result]
    if {x, y - 1} in list, do: result, else: [:left | result]
  end

  defp count_sides(chunks, direction, index) do
    Enum.reduce(chunks, 0, fn chunk, acc -&gt;
      acc +
        case Enum.filter(chunk, fn {_point, fences} -&gt; direction in fences end) do
          [] -&gt;
            0

          [chunk_up_head | chunk_up_rest] -&gt;
            {point, _} = chunk_up_head
            e = elem(point, index)

            {_, counter} =
              Enum.reduce(chunk_up_rest, {e, 1}, fn {point, _}, {tracker, counter} -&gt;
                if elem(point, index) + 1 == tracker do
                  {tracker - 1, counter}
                else
                  {elem(point, index), counter + 1}
                end
              end)

            counter
        end
    end)
  end
end
</code></pre>
<p>Also, I stumbled upon a behavior which was unexpected to me while working on this, regarding group_by/chunk_by.<br>
I made a post <a href="https://forum.elixirforum.com/t/difference-between-group-by-and-chunk-by/68112" rel="nofollow">here</a> about it. <img src="https://forum.elixirforum.com/images/emoji/apple/nerd_face.png?v=15" title=":nerd_face:" class="emoji" alt=":nerd_face:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349880" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-12/68054/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-349880" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349880"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #40"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/68054/load_more?page=5">Load more posts (1 remaining)</a>
</div></template></turbo-stream>