<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="348511" data-post-id="348511">
  <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>super inefficient … but did you have fun writing it? <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348511" 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-4/67869/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-348511" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348511"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="348512" data-post-id="348512">
  <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>Haha yes I did, thanks! I think it’s fun to see how short I can make the solutions rather than how fast, so that’s what I’m doing this year.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Before doing the challenge I decided to check which data structure would be ideal for fast random access and binaries won, so I based my code on that.</p>
<h3><a name="p-348527-benchmark-of-randomly-accessing-every-letter-of-the-first-5-paragraphs-of-lorem-ipsum-in-a-random-order-1" class="anchor" href="#p-348527-benchmark-of-randomly-accessing-every-letter-of-the-first-5-paragraphs-of-lorem-ipsum-in-a-random-order-1" aria-label="Heading link" rel="nofollow"></a>Benchmark of randomly accessing every letter of the first 5 paragraphs of lorem ipsum in a random order</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">Name                          ips        average  deviation         median         99th %
binary                     7.92 K      126.33 μs     ±6.69%      124.37 μs      149.82 μs
tuple random access        6.12 K      163.31 μs     ±6.54%      160.95 μs      193.62 μs
list                       5.36 K      186.71 μs     ±5.73%      184.37 μs      211.32 μs

Comparison:
binary                     7.92 K
tuple random access        6.12 K - 1.29x slower +36.98 μs
list                       5.36 K - 1.48x slower +60.38 μs
</code></pre>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/Awlexus/b4f79a43d5e1c3a3fee16dff6b28c950">
  <header class="source">

      <a href="https://gist.github.com/Awlexus/b4f79a43d5e1c3a3fee16dff6b28c950" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/Awlexus/b4f79a43d5e1c3a3fee16dff6b28c950" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/Awlexus/b4f79a43d5e1c3a3fee16dff6b28c950</a></h4>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Time to solve on my machine:<br>
Day 1: 7.348 ms<br>
Day 2: 1.04 ms</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So there’s another one. Didn’t bother to check performance while writing but did care about clarity of the code and flexibility. It’s not Xmas every day…</p>
<p>Would like to compare performance, but most post don’t have timing information.</p>
<p><strong>Solution for 2024 day 4</strong><br>
part_one: 2536 in 22.95ms<br>
part_two: 1875 in 15.2ms</p>
<p><strong>Improvements possible:</strong></p>
<ul>
<li>district horizontal and vertical movement</li>
<li>proper names for directions instead of [-1,1,0]</li>
<li>refactor some ugly property drilling <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"></li>
<li>refactor all function at the bottom…as they are…different.</li>
</ul>
<p><strong>Code!</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day04 do
  alias AoC.Input

  def part_one(problem) do
    grid = to_grid(problem)
    coordinates = find_coordinates(grid, "XMAS")

    Enum.count(coordinates)
  end

  def part_two(problem) do
    grid = to_grid(problem)
    coordinates = find_coordinates(grid, "MAS", directions: [-1, 1])
    cross_coordinates = find_crosses(coordinates, fn [_, pos_a, _] -&gt; pos_a end)

    Enum.count(cross_coordinates)
  end

  defp find_coordinates(grid, &lt;&lt;marker::utf8&gt;&gt; &lt;&gt; _ = word, opts \\ []) do
    grid
    |&gt; list_marker_positions(&lt;&lt;marker&gt;&gt;)
    |&gt; Enum.map(&amp;generate_candidates(&amp;1, String.length(word), opts))
    |&gt; find_word_in_candidates(grid, word)
  end

  defp list_marker_positions(grid, marker) do
    for {row, cols} &lt;- grid, {col, ^marker} &lt;- cols do
      {row, col}
    end
  end

  defp generate_candidates({row, col} = _pos, length, opts) do
    directions = Keyword.get(opts, :directions, [-1, 1, 0])

    for row_shift &lt;- directions, col_shift &lt;- directions do
      for shift &lt;- 0..(length - 1) do
        {row + shift * row_shift, col + shift * col_shift}
      end
    end
  end

  defp find_crosses(coordinates, group_fun) do
    coordinates
    |&gt; Enum.group_by(&amp;group_fun.(&amp;1))
    |&gt; Enum.filter(fn {_, candidates} -&gt; candidates |&gt; Enum.count() &gt;= 2 end)
  end

  defp find_word_in_candidates(candidates, grid, word) do
    wordlist = String.split(word, "", trim: true)

    for candidate &lt;- candidates, positions &lt;- candidate, reduce: [] do
      acc -&gt;
        result =
          for {row, col} &lt;- positions do
            grid[row][col]
          end

        (result == wordlist &amp;&amp; [positions | acc]) || acc
    end
  end

  defp to_grid(input) do
    for row &lt;- input do
      String.split(row, "", trim: true) |&gt; to_index_map()
    end
    |&gt; to_index_map()
  end

  defp to_index_map(input) do
    input |&gt; Enum.with_index(fn el, col_idx -&gt; {col_idx, el} end) |&gt; Map.new()
  end

  def parse(input, _part) do
    input
    |&gt; Input.stream!(trim: true)
  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="348546" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-348546" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348546"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="348547" data-post-id="348547">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Had to get some sleep and deal with work today. Catching up with this late, but still day of.</p>
<p>Part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Day4.Part1 do
  defp transpose(%{rows: rows} = haystack) do
    %{haystack | rows: Enum.zip(rows) |&gt; Enum.map(&amp;Tuple.to_list/1)}
  end

  defp count_linear(row, needle, length) do
    stride = needle |&gt; Enum.count()

    for x &lt;- 0..(length - stride) do
      slice = Enum.slice(row, x..(x + stride - 1))
      equal? = slice == needle
      if equal?, do: 1, else: 0
    end
    |&gt; Enum.sum()
  end

  defp count_horizontal(%{rows: rows, length: length}, needle) do
    rows
    |&gt; Enum.reduce(
      0,
      fn row, acc -&gt;
        acc + count_linear(row, needle, length) + count_linear(Enum.reverse(row), needle, length)
      end
    )
  end

  defp count_vertical(haystack, needle) do
    count_horizontal(transpose(haystack), needle)
  end

  defp skew(%{rows: rows, length: length} = haystack) do
    %{
      haystack
      | rows:
          for(_ &lt;- 1..(length - 1), do: for(_ &lt;- 1..(length + length - 1), do: nil)) ++
            (rows
             |&gt; Enum.reduce(
               %{i: 0, j: length - 1, rows: []},
               fn row, state -&gt;
                 %{
                   i: state.i + 1,
                   j: state.j - 1,
                   rows: [
                     if(state.i == 0, do: [], else: for(_ &lt;- 1..state.i, do: nil)) ++
                       row ++ if(state.j == 0, do: [], else: for(_ &lt;- 1..state.j, do: nil))
                     | state.rows
                   ]
                 }
               end
             )
             |&gt; Map.get(:rows)
             |&gt; Enum.reverse()),
        length: length + length - 1
    }
  end

  defp count_diagonal(%{rows: rows} = haystack, needle) do
    backslash_count =
      haystack
      |&gt; skew()
      |&gt; count_vertical(needle)

    forward_slash_count =
      %{haystack | rows: rows |&gt; Enum.map(&amp;Enum.reverse/1)}
      |&gt; skew()
      |&gt; count_vertical(needle)

    backslash_count + forward_slash_count
  end

  defp count(haystack, needle) do
    count_horizontal(haystack, needle) + count_vertical(haystack, needle) +
      count_diagonal(haystack, needle)
  end

  defp parse(str) do
    lines =
      str
      |&gt; String.split("\n")

    rowcount =
      lines
      |&gt; Enum.count()

    rows =
      lines
      |&gt; Enum.map(&amp;to_charlist/1)

    length = rows |&gt; List.first() |&gt; Enum.count()

    ^length = rowcount

    %{
      length: length,
      rows: rows
    }
  end

  def solve() do
    needle = ~c"XMAS"

    haystack =
      File.read!("04/input.txt")
      |&gt; parse()

    count(haystack, needle)
    |&gt; IO.puts()
  end
end

Day4.Part1.solve()
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Day4.Part2 do
  @mas ~c"MAS"

  defp count_crosses([a | [_a_2 | [a_3 | _a_t]] = tail_a], [_b | [b_2 | [_b_3 | _b_t]] = tail_b], [
         c | [_c_2 | [c_3 | _c_t]] = tail_c
       ]) do
    top_down_backslash? = [a, b_2, c_3] == @mas
    bottom_up_backslash? = [c_3, b_2, a] == @mas
    top_down_forward_slash? = [a_3, b_2, c] == @mas
    bottom_up_forward_slash? = [c, b_2, a_3] == @mas

    if (top_down_backslash? and bottom_up_forward_slash?) or
         (bottom_up_backslash? and bottom_up_forward_slash?) or
         (top_down_backslash? and top_down_forward_slash?) or
         (bottom_up_backslash? and top_down_forward_slash?) do
      1
    else
      0
    end
    |&gt; (fn x -&gt; x + count_crosses(tail_a, tail_b, tail_c) end).()
  end

  defp count_crosses([_, _], [_, _], [_, _]), do: 0

  defp count_mas([row_a | [row_b | [row_c | _t]] = tail]) do
    count_crosses(row_a, row_b, row_c) + count_mas(tail)
  end

  defp count_mas([_row_a , _row_b]), do: 0

  defp parse(str) do
    str
    |&gt; String.split("\n")
    |&gt; Enum.map(&amp;to_charlist/1)
  end

  def solve() do
    haystack =
      File.read!("04/input.txt")
      |&gt; parse()

    count_mas(haystack)
    |&gt; IO.puts()
  end
end

Day4.Part2.solve()
</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="348547" 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/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-348547" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348547"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="348548" data-post-id="348548">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sevenseacat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sevenseacat/120/23153_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sevenseacat
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Ash Framework</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I do benchmarking for all of my solutions - here’s my 2024 day 4 <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>
<pre data-code-wrap="elixir"><code class="lang-elixir">Name                     ips        average  deviation         median         99th %
day 04, part 1       0.109 K     9206.98 μs     ±3.53%     9049.48 μs    10099.13 μs
day 04, part 2       0.120 K     8363.73 μs     ±4.76%     8099.29 μs     9391.84 μs
</code></pre>
<p>That was on my M1 Max though…</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Saw a meme about day 4 on <code>r/adventofcode</code>, so my part 1 solution was somewhat overengineered - but it was an easy jump to part 2 as a result.</p>
<p>For fun, tried to write the whole thing in a local-variable-free style, which makes for very arrow-shaped functions.</p>
<p>Part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PatternFinder do
  defmodule Grid do
    defstruct [:by_char, :by_pos]

    def init do
      %__MODULE__{
        by_char: %{},
        by_pos: %{}
      }
    end

    def add(g, char, pos) do
      %{g |
        by_char: Map.update(g.by_char, char, MapSet.new([pos]), &amp;MapSet.put(&amp;1, pos)),
        by_pos: Map.put(g.by_pos, pos, char)
      }
    end

    def get(g, pos) do
      Map.fetch(g.by_pos, pos)
    end

    def where(g, char) do
      Map.fetch!(g.by_char, char)
    end
  end

  def read(filename) do
    File.stream!(filename)
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;String.graphemes/1)
    |&gt; Stream.with_index()
    |&gt; Stream.flat_map(fn {chars, row} -&gt;
      chars
      |&gt; Enum.with_index()
      |&gt; Enum.map(fn {c, col} -&gt;
        {{row, col}, c}
      end)
    end)
    |&gt; Enum.reduce(Grid.init(), fn {pos, char}, grid -&gt;
      Grid.add(grid, char, pos)
    end)
  end

  @patterns [
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {0,1},
        "A" =&gt; {0,2},
        "S" =&gt; {0,3}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {0,-1},
        "A" =&gt; {0,-2},
        "S" =&gt; {0,-3}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {1,0},
        "A" =&gt; {2,0},
        "S" =&gt; {3,0}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {-1,0},
        "A" =&gt; {-2,0},
        "S" =&gt; {-3,0}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {1,1},
        "A" =&gt; {2,2},
        "S" =&gt; {3,3}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {-1,1},
        "A" =&gt; {-2,2},
        "S" =&gt; {-3,3}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {1,-1},
        "A" =&gt; {2,-2},
        "S" =&gt; {3,-3}
      }
    },
    %{
      start_at: "X",
      neighbors: %{
        "M" =&gt; {-1,-1},
        "A" =&gt; {-2,-2},
        "S" =&gt; {-3,-3}
      }
    },
  ]

  def matches(grid) do
    @patterns
    |&gt; Enum.flat_map(fn pattern -&gt;
      grid
      |&gt; Grid.where(pattern.start_at)
      |&gt; Enum.filter(fn {row, col} -&gt;
        pattern.neighbors
        |&gt; Enum.all?(fn {expected_char, {row_off, col_off}} -&gt;
          case Grid.get(grid, {row + row_off, col + col_off}) do
            :error -&gt; false
            {:ok, char} -&gt; char == expected_char
          end
        end)
      end)
    end)
  end
end

PatternFinder.read("input.txt")
|&gt; PatternFinder.matches()
|&gt; IO.inspect()
|&gt; length()
|&gt; IO.inspect()
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PatternFinder do
  defmodule Grid do
    defstruct [:by_char, :by_pos]

    def init do
      %__MODULE__{
        by_char: %{},
        by_pos: %{}
      }
    end

    def add(g, char, pos) do
      %{g |
        by_char: Map.update(g.by_char, char, MapSet.new([pos]), &amp;MapSet.put(&amp;1, pos)),
        by_pos: Map.put(g.by_pos, pos, char)
      }
    end

    def get(g, pos) do
      Map.fetch(g.by_pos, pos)
    end

    def where(g, char) do
      Map.fetch!(g.by_char, char)
    end
  end

  def read(filename) do
    File.stream!(filename)
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;String.graphemes/1)
    |&gt; Stream.with_index()
    |&gt; Stream.flat_map(fn {chars, row} -&gt;
      chars
      |&gt; Enum.with_index()
      |&gt; Enum.map(fn {c, col} -&gt;
        {{row, col}, c}
      end)
    end)
    |&gt; Enum.reduce(Grid.init(), fn {pos, char}, grid -&gt;
      Grid.add(grid, char, pos)
    end)
  end

  @patterns [
    %{
      start_at: "A",
      neighbors: %{
        {-1,-1} =&gt; "M",
        {1,-1} =&gt; "M",
        {-1,1} =&gt; "S",
        {1,1} =&gt; "S"
      }
    },
    %{
      start_at: "A",
      neighbors: %{
        {-1,-1} =&gt; "S",
        {1,-1} =&gt; "M",
        {-1,1} =&gt; "S",
        {1,1} =&gt; "M"
      }
    },
    %{
      start_at: "A",
      neighbors: %{
        {-1,-1} =&gt; "S",
        {1,-1} =&gt; "S",
        {-1,1} =&gt; "M",
        {1,1} =&gt; "M"
      }
    },
    %{
      start_at: "A",
      neighbors: %{
        {-1,-1} =&gt; "M",
        {1,-1} =&gt; "S",
        {-1,1} =&gt; "M",
        {1,1} =&gt; "S"
      }
    },
  ]

  def matches(grid) do
    @patterns
    |&gt; Enum.flat_map(fn pattern -&gt;
      grid
      |&gt; Grid.where(pattern.start_at)
      |&gt; Enum.filter(fn {row, col} -&gt;
        pattern.neighbors
        |&gt; Enum.all?(fn {{row_off, col_off}, expected_char} -&gt;
          case Grid.get(grid, {row + row_off, col + col_off}) do
            :error -&gt; false
            {:ok, char} -&gt; char == expected_char
          end
        end)
      end)
    end)
  end
end

PatternFinder.read("input.txt")
|&gt; PatternFinder.matches()
|&gt; IO.inspect()
|&gt; length()
|&gt; IO.inspect()
</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="348550" 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/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-348550" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348550"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="348560" data-post-id="348560">
  <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>Part1 was so hard! I think i must have digged my self into a rabbit hole or something, because the natural “human approach” is often translatable into a program.</p>
<p>First i started to look for horizontals and verticals, and made a count function for both. Easy. Then i made a new function for counting diagonals. Thats where the hell began. But i still dont know why.</p>
<p>Anyway. Due to me wanting total control and be able to cross reference, my program outputs this</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  {0, 4, 3, 7, "XMAS"},
  {0, 5, 0, 8, "XMAS"},
  {1, 1, 1, 4, "SAMX"},
  {1, 6, 4, 6, "SAMX"},
  {2, 3, 5, 6, "SAMX"},
  {2, 3, 5, 0, "SAMX"},
  {3, 9, 6, 9, "XMAS"},
  {3, 9, 6, 6, "XMAS"},
  {4, 0, 4, 3, "XMAS"},
  {4, 3, 4, 6, "SAMX"},
  {6, 0, 9, 3, "SAMX"},
  {6, 2, 9, 5, "SAMX"},
  {6, 4, 9, 1, "SAMX"},
  {6, 6, 9, 9, "SAMX"},
  {6, 6, 9, 3, "SAMX"},
  {6, 8, 9, 5, "SAMX"},
  {6, 9, 9, 9, "SAMX"},
  {9, 5, 9, 8, "XMAS"}
]
</code></pre>
<p>which is start row id, start col id, end row id, end col id and the word it found</p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_4/Part1.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_4/Part1.ex</a></p>
<p>i almost never publishes unpolished code like this, but i cant stand working on it more <img src="https://forum.elixirforum.com/images/emoji/apple/sob.png?v=15" title=":sob:" class="emoji" alt=":sob:" 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="348560" 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/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-348560" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348560"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="348569" data-post-id="348569">
  <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>at least part2 didnt kill my soul</p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_4/Part2.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_4/Part2.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="348569" 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/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-348569" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348569"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="348571" data-post-id="348571">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="BartOtten" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/BartOtten/120/25440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  BartOtten
                  </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="29" 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/jarlah/48/24789_2.png" class="avatar"> jarlah:</div>
<blockquote>
<p>Part1 was so hard! I think i must have digged my self into a rabbit hole or something</p>
</blockquote>
</aside>
<p>Looking at your code I see which rabbit hole you experienced. I experienced the same and just dropped all code and started from scratch.</p>
<p>You made it, which is the most important <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="348571" data-batch-url="/posts/batch_likers">
                        3
                      </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/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-348571" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348571"
                     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 #30"></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=4">Load more posts (5 remaining)</a>
</div></template></turbo-stream>