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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Nothing too fancy. I did some basic optimizations like checking only from <code>X</code> (in p1) and <code>A</code> (in p2), plus <code>reduce_while</code> in p1 to stop early if there’s no matches.</p>
<p><a href="https://github.com/mexicat/aoc-2024/blob/main/lib/aoc/day_04.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/mexicat/aoc-2024/blob/main/lib/aoc/day_04.ex</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348431" 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-4/67869/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-348431" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348431"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="348443" data-post-id="348443">
  <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>Added some padding and made sub matrixes. Not optimal at all. <img src="https://forum.elixirforum.com/images/emoji/apple/weary.png?v=15" title=":weary:" class="emoji" alt=":weary:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day04 do
  alias AoC.Input

  def parse(input, part) do
    input =
      input
      |&gt; Input.stream!(trim: true)
      |&gt; Enum.map(&amp;String.codepoints(&amp;1 &lt;&gt; "YYY"))
      |&gt; (fn input -&gt; input ++ padding(length(hd(input))) end).()

    n = if part == :part_one, do: 4, else: 3

    for x &lt;- 0..(length(input) - n), y &lt;- 0..(length(hd(input)) - n) do
      {_, rest_x} = Enum.split(input, x)
      rest_rows = Enum.take(rest_x, n)

      Enum.map(rest_rows, fn rest_row -&gt;
        {_, rest_y} = Enum.split(rest_row, y)
        Enum.take(rest_y, n)
      end)
    end
  end

  def part_one(problem), do: Enum.reduce(problem, 0, &amp;(&amp;2 + count_xmas(&amp;1)))

  def part_two(problem), do: Enum.reduce(problem, 0, &amp;(&amp;2 + count_mas(&amp;1)))

  defp count_xmas([[a1, a2, a3, a4], [b1, b2, b3, _b4], [c1, c2, c3, _c4], [d1, _d2, _d3, d4]]) do
    list = [
      a1 &lt;&gt; a2 &lt;&gt; a3 &lt;&gt; a4,
      a1 &lt;&gt; b1 &lt;&gt; c1 &lt;&gt; d1,
      a1 &lt;&gt; b2 &lt;&gt; c3 &lt;&gt; d4,
      a4 &lt;&gt; b3 &lt;&gt; c2 &lt;&gt; d1
    ]

    Enum.reduce(list, 0, fn e, acc -&gt;
      case e do
        "XMAS" -&gt; acc + 1
        "SAMX" -&gt; acc + 1
        _ -&gt; acc
      end
    end)
  end

  defp count_mas([[a1, _a2, a3], [_b1, b2, _b3], [c1, _c3, c3]]) do
    case b2 &lt;&gt; a1 &lt;&gt; a3 &lt;&gt; c1 &lt;&gt; c3 do
      "AMMSS" -&gt; 1
      "ASSMM" -&gt; 1
      "ASMSM" -&gt; 1
      "AMSMS" -&gt; 1
      _ -&gt; 0
    end
  end

  defp padding(n) do
    for _ &lt;- 0..2 do
      for _ &lt;- 0..(n - 1) do
        "Y"
      end
    end
  end
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">Solution for 2024 day 4
part_one: 2427 in 82.75ms
part_two: 1900 in 49.76ms
</code></pre>
<p>I could try nx now that I thought about it… <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" 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="348443" 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-4/67869/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-348443" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348443"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="348453" data-post-id="348453">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ugh. Grids. Just grunt work.</p>
<p>I decided to use <code>:array</code> to represent the grid as I know the O(log n) lookup of maps are not ideal. Combined with <code>for</code>, it’s not very elixir-like. I agree with <a class="mention" href="/u/lkuty" rel="nofollow">@lkuty</a> that native code is probably the way to go for this kind of thing. I might come back to this when I have more free time next year to try it with Zigler.</p>
<p>Four of these for part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for y &lt;- 0..(n - 1), x &lt;- 0..(n - 4), reduce: 0 do
  count -&gt;
    for x_offset &lt;- 0..(4 - 1), into: "" do
      :array.get(x + x_offset, :array.get(y, grid))
    end
    |&gt; consider(count)
end
</code></pre>
<p>and some similar stuff in part 2.</p>
<p>On my M1 the times were 15ms and 4ms. Not sure how that compares to the Map solutions.</p>
<p><a href="https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2024/day4.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2024/day4.exs</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348453" 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-4/67869/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-348453" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348453"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="348454" data-post-id="348454">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It took me some time to get to the solution today. I solved part 2 by counting the diagonal occurrences of “MAS” in the 3x3 grid around the current position. If there are more than one then it is a valid X. I wondered if it was possible to have more than two “MAS” occurrences, but it is not because if two edges start with “M” then the other two edges must end with “S” obviously <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<p>For both solutions, I construct the full word / sequence before comparing it with “MAS” or "XMAS. An optimization would be to stop earlier, e.g., if the word doesn’t start with X or M at all…</p>
<p>Source Code:</p>
<p><a href="https://github.com/Flo0807/adventofcode/blob/main/2024/04.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/Flo0807/adventofcode/blob/main/2024/04.livemd</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348454" 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-4/67869/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-348454" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348454"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="348462" data-post-id="348462">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adamu" data-post="14" data-topic="67869">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/48/31482_2.png" class="avatar"> adamu:</div>
<blockquote>
<p>I decided to use <code>:array</code> to represent the grid</p>
</blockquote>
</aside>
<p>It looks like Erlang arrays use a tree (trie maybe?) data structure and have O(log n) time complexity. See <a href="https://github.com/erlang/otp/blob/44ffe8811dfcf3d2fe04d530c6e8fac5ca384e02/lib/stdlib/src/array.erl#L126" class="inline-onebox" rel="noopener nofollow ugc">otp/lib/stdlib/src/array.erl at 44ffe8811dfcf3d2fe04d530c6e8fac5ca384e02 · erlang/otp · GitHub</a></p>
<p>I “cheated” a little bit because the C implementation is not just for 2D arrays of ints but it is specifically made for that problem. It allows to avoid many round-trips between Elixir and C as we check if we have an XMAS candidate. I could have gone all-in with C but then the Elixir code would have just been some sort of a driver for the C code. Would have been faster for sure.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348462" 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-4/67869/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-348462" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348462"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="348463" data-post-id="348463">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Haha, oh well. I wonder why that info hidden in a comment instead of being in the moduledocs (which I read before starting, but didn’t find any information about performance…)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348463" 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-4/67869/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-348463" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348463"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="348466" data-post-id="348466">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes, it should be in the doc of the <code>arrays</code> module. This is an important info.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348466" 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-4/67869/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-348466" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348466"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="348479" data-post-id="348479">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For part 1 I rotates the matrix instead of looking in differente directions.<br>
And for part 2, I went with a sliding window using Enum,chunks =)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule Smaoc.Solution.Day4 do
  def solve(:part1, input) do
    matrix = parse_input(input)

    [
      [],
      [:reverse],
      [:transpose],
      [:transpose, :reverse],
      [:rotate45],
      [:rotate45, :reverse],
      [:reverse, :rotate45],
      [:reverse, :rotate45, :reverse]
    ]
    |&gt; Enum.map(
      &amp;(matrix
        |&gt; apply_operations(&amp;1)
        |&gt; count_XMAS)
    )
    |&gt; Enum.sum()
  end

  def solve(:part2, input) do
    valid_patterns = [
      ~r/M.M.A.S.S/,
      ~r/M.S.A.M.S/,
      ~r/S.M.A.S.M/,
      ~r/S.S.A.M.M/
    ]

    input
    |&gt; parse_input()
    |&gt; Enum.chunk_every(3, 1, :discard)
    |&gt; Enum.map(fn row -&gt;
      row
      |&gt; transpose()
      |&gt; Enum.chunk_every(3, 1, :discard)
      |&gt; Enum.count(fn chunk -&gt;
        Enum.any?(valid_patterns, &amp;Regex.match?(&amp;1, to_string(chunk)))
      end)
    end)
    |&gt; Enum.sum()
  end

  defp parse_input(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;String.split(&amp;1, "", trim: true))
  end

  def reverse(matrix), do: Enum.map(matrix, &amp;Enum.reverse/1)

  def transpose(matrix) do
    matrix
    |&gt; Enum.zip()
    |&gt; Enum.map(&amp;Tuple.to_list/1)
  end

  def rotate45(matrix) do
    for(
      {row, i} &lt;- Enum.with_index(matrix),
      {v, j} &lt;- Enum.with_index(row),
      do: {i + j, v}
    )
    |&gt; Enum.group_by(&amp;elem(&amp;1, 0), &amp;elem(&amp;1, 1))
    |&gt; Enum.sort()
    |&gt; Enum.map(&amp;elem(&amp;1, 1))
  end

  def count_XMAS(matrix) do
    matrix
    |&gt; Enum.map(fn l -&gt;
      ~r/XMAS/
      |&gt; Regex.scan(to_string(l))
      |&gt; Enum.count()
    end)
    |&gt; Enum.sum()
  end

  defp apply_operations(matrix, ops) do
    Enum.reduce(ops, matrix, fn op, m -&gt; apply(__MODULE__, op, [m]) end)
  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="348479" 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-4/67869/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-348479" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348479"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="348480" data-post-id="348480">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s mine for today, it’s a bit verbose, so could be tidied up significantly, agreed with others that Part 2 seemed easier than Part 1 in some ways, really waiting for the hammer to drop as the first 4 days have seemed worryingly easy compared to last year.</p>
<pre><code>    defmodule Aoc2024.Solutions.Y24.Day04 do
      alias AoC.Input

      def parse(input, _part) do
        Input.read!(input) |&gt; String.split("\n", trim: true) |&gt; pad_grid()
      end

      def part_one(problem) do
        matrix = problem |&gt; build_matrix()
        matrix |&gt; Enum.to_list() |&gt; Enum.filter(fn {_, v} -&gt; v == "X" end) |&gt; find_xmas(matrix)
      end

      def find_xmas(xs, matrix) do
        Enum.reduce(xs, [], fn {x_coords, _}, acc -&gt;
          acc ++
            [
              find_next(x_coords, :up, ["M", "A", "S"], matrix),
              find_next(x_coords, :down, ["M", "A", "S"], matrix),
              find_next(x_coords, :left, ["M", "A", "S"], matrix),
              find_next(x_coords, :right, ["M", "A", "S"], matrix),
              find_next(x_coords, :diag_up_right, ["M", "A", "S"], matrix),
              find_next(x_coords, :diag_down_right, ["M", "A", "S"], matrix),
              find_next(x_coords, :diag_up_left, ["M", "A", "S"], matrix),
              find_next(x_coords, :diag_down_left, ["M", "A", "S"], matrix)
            ]
        end)
        |&gt; Enum.filter(&amp;(&amp;1 == 1))
        |&gt; Enum.count()
      end

      def find_next(_, _, [], _), do: 1

      def find_next({current_x, current_y}, direction, [next_letter | tail], matrix) do
        next_coords = get_next_coord({current_x, current_y}, direction)
        next_match = Map.get(matrix, next_coords)

        if next_letter == next_match do
          find_next(next_coords, direction, tail, matrix)
        else
          0
        end
      end

      def pad_grid(problem) do
        [first | _] = problem
        width = String.length(first)
        pad_row = 1..width |&gt; Enum.map(fn _ -&gt; "." end) |&gt; Enum.join()
        [pad_row] ++ Enum.map(problem, &amp;".#{&amp;1}.") ++ [pad_row]
      end

      #

      def build_matrix(grid) do
        Enum.with_index(grid)
        |&gt; Enum.reduce(%{}, fn row, acc -&gt;
          build_matrix_row(row, acc)
        end)
      end

      def build_matrix_row({row, row_index}, acc) do
        row
        |&gt; String.graphemes()
        |&gt; Enum.with_index()
        |&gt; Enum.reduce(acc, fn {char, col_index}, acc -&gt;
          Map.put(acc, {col_index, row_index}, char)
        end)
      end

      def get_next_coord({current_x, current_y}, direction) do
        case direction do
          :up -&gt; {current_x, current_y - 1}
          :down -&gt; {current_x, current_y + 1}
          :left -&gt; {current_x - 1, current_y}
          :right -&gt; {current_x + 1, current_y}
          :diag_up_right -&gt; {current_x + 1, current_y - 1}
          :diag_down_right -&gt; {current_x + 1, current_y + 1}
          :diag_up_left -&gt; {current_x - 1, current_y - 1}
          :diag_down_left -&gt; {current_x - 1, current_y + 1}
        end
      end

      def part_two(problem) do
        matrix = problem |&gt; build_matrix()
        matrix |&gt; Enum.to_list() |&gt; Enum.filter(fn {_, v} -&gt; v == "A" end) |&gt; find_x_mases(matrix)
      end

      def find_x_mases(as, matrix) do
        Enum.reduce(as, [], fn a, acc -&gt;
          acc ++ [find_x_mas(a, matrix)]
        end)
        |&gt; Enum.filter(&amp; &amp;1)
        |&gt; Enum.count()
      end

      def find_x_mas({{coord_x, coord_y}, _}, matrix) do
        cross_1 =
          [
            Map.get(matrix, {coord_x - 1, coord_y - 1}),
            Map.get(matrix, {coord_x, coord_y}),
            Map.get(matrix, {coord_x + 1, coord_y + 1})
          ]
          |&gt; List.to_string()

        cross_2 =
          [
            Map.get(matrix, {coord_x + 1, coord_y - 1}),
            Map.get(matrix, {coord_x, coord_y}),
            Map.get(matrix, {coord_x - 1, coord_y + 1})
          ]
          |&gt; List.to_string()

        cross_1 in ["MAS", "SAM"] &amp;&amp; cross_2 in ["MAS", "SAM"]
      end
    end
</code></pre>
<p>Solution for 2024 day 4<br>
part_one: 2370 in 143.32ms<br>
part_two: 1908 in 26.88ms</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348480" 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-4/67869/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-348480" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348480"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="348510" data-post-id="348510">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Approach: independent streams for each letter of the word and a bunch of zips.<br>
Notes: super inefficient.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day04 do
  def part1(f), do: count(f, "XMAS", [[0, 1], [1, 0], [1, 1], [1, -1]], &amp;Enum.count/2)
  def part2(f), do: count(f, "MAS", [[1, 1], [1, -1]], fn x, y -&gt; (Enum.all?(x, y) &amp;&amp; 1) || 0 end)

  def count(file, word, jumps, fun) do
    stream = File.stream!(file, 1)
    {w, l} = {Enum.find_index(stream, &amp;(&amp;1 == "\n")), String.length(word)}
    stream = Stream.concat(Stream.reject(stream, &amp;(&amp;1 == "\n")), Stream.duplicate("", w * l))

    Enum.map(jumps, fn [dr, dc] -&gt;
      Enum.map(0..(String.length(word) - 1), fn j -&gt;
        Stream.drop(stream, max(0, -1 - l * dc) + w * (j * dr) + j * dc)
        |&gt; Stream.transform(max(0, -1 - l * dc), fn char, i -&gt;
          x = Enum.all?([div(i, w) + j * dr, rem(i, w) + j * dc], &amp;(&amp;1 in 0..(w - 1)))
          {[if(x, do: char, else: "")], i + 1}
        end)
      end)
      |&gt; Stream.zip_with(&amp;Enum.join/1)
    end)
    |&gt; Enum.zip_reduce(0, fn x, s -&gt; s + fun.(x, &amp;(&amp;1 in [word, String.reverse(word)])) end)
  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="348510" 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-4/67869/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-348510" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348510"
                     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 #20"></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/67869/load_more?page=3">Load more posts (15 remaining)</a>
</div></template></turbo-stream>