<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="270780" data-post-id="270780">
  <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>Even in Elixir, for small lists directly comparing the items with each other is much faster than using a map to store seen elements (which is what <code>Enum.uniq</code> does).</p>
<p><a class="mention" href="/u/bmitc" rel="nofollow">@bmitc</a>’s function is 9x slower than the direct comparison of 4 items on my machine.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Comparison:
direct_compare                          17.22 K
find_end_of_first_distinct_window        1.86 K - 9.28x slower +481.00 μs
</code></pre>
<p>However, the number of comparisons increases by n-1 for each element added, so there’s probably a point where using a map is faster.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270780" 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-2022-day-6/52286/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-270780" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270780"
                     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="270912" data-post-id="270912">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Part1 do

  defguard is_start_of_packet_marker?(a, b, c, d) when a != b and a != c and a != d and b != c and b != d and c != d

  def find_marker(signal) do
    do_find_marker(signal, 0)
  end

  defp do_find_marker(&lt;&lt;a, b, c, d, _rest::binary&gt;&gt;, index) when is_start_of_packet_marker?(a, b, c, d) do
    index + 4
  end

  defp do_find_marker(&lt;&lt;_a, b, c, d, rest::binary&gt;&gt;, index) do
    do_find_marker(&lt;&lt;b, c, d, rest::binary&gt;&gt;, index + 1)
  end
end

Part1.find_marker(input)
</code></pre>
<p>Part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Part2 do

  def find_message(signal) do
    do_find_message(signal, 0)
  end

  defp do_find_message(&lt;&lt;first, chars::binary-size(13), rest::binary&gt;&gt;, index) do
    case is_message?(&lt;&lt;first, chars::binary-size(13)&gt;&gt;) do
      true -&gt; index + 14
      false -&gt; do_find_message(&lt;&lt;chars::binary-size(13), rest::binary&gt;&gt;, index + 1)
    end
  end

  defp is_message?(chars) do
    chars
    |&gt; String.codepoints()
    |&gt; Enum.uniq()
    |&gt; then(fn unique_chars -&gt; length(unique_chars) == byte_size(chars) end)
  end
end

Part2.find_message(input)
</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="270912" 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-2022-day-6/52286/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-270912" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270912"
                     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="271275" data-post-id="271275">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This puzzle was easy, I decided to go with <code>Enum.reduce_while/3</code>.</p>
<p><a href="https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day6.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day6.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="271275" 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-2022-day-6/52286/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-271275" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="271275"
                     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="287666" data-post-id="287666">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Our company is switching to Elixir so I’m pretty new to the language and like to use the Advent puzzles as a learning tool.</p>
<p>My first solution was reasonably fast using a MapSet, but then I saw <a href="https://www.youtube.com/watch?v=U16RnpV48KQ" rel="noopener nofollow ugc">this video</a> and came up with this. It runs in 200 µs here on part 2.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day6 do
  def read!(filename) do
    File.read!(filename)
    |&gt; String.to_charlist()
  end

  def find_marker(data, count, index \\ 0) do
    chunk =
      Enum.slice(data, 0, count)
      |&gt; Enum.reverse()

    case check_chunk(chunk, count, 0) do
      :ok -&gt;
        index + count

      {:error, fail_index} -&gt;
        find_marker(Enum.slice(data, fail_index..-1), count, index + fail_index)
    end
  end

  defp check_chunk(_chunk, 0, _checker), do: :ok

  defp check_chunk([char | chunk], count, checker) do
    char = Bitwise.bsl(1, rem(char, 32))

    case new_check = Bitwise.bor(checker, char) do
      ^checker -&gt; {:error, count}
      _ -&gt; check_chunk(chunk, count - 1, new_check)
    end
  end
end

:timer.tc(fn -&gt;
  Day6.read!(filename)
  |&gt; Day6.find_marker(4)
end)
|&gt; IO.inspect(label: "Part 1")

:timer.tc(fn -&gt;
  Day6.read!(filename)
  |&gt; Day6.find_marker(14)
end)
|&gt; IO.inspect(label: "Part 2")
</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="287666" 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-2022-day-6/52286/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-287666" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287666"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>