<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="348766" data-post-id="348766">
  <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>My first attempt worked but part 2 took more than 100 seconds. <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"><br>
So I got inspired by <a class="mention" href="/u/aetherus" rel="nofollow">@Aetherus</a> and <a class="mention" href="/u/bjorng" rel="nofollow">@bjorng</a> 's code and tips to rewrite it better. <img src="https://forum.elixirforum.com/images/emoji/apple/muscle.png?v=15" title=":muscle:" class="emoji" alt=":muscle:" loading="lazy" width="20" height="20"><br>
For instance:</p>
<blockquote>
<p>putting obstacles only in the path actually walked by the guard.</p>
</blockquote>
<p>And using MapSet but I reduced the streams. <img src="https://forum.elixirforum.com/images/emoji/apple/cowboy_hat_face.png?v=15" title=":cowboy_hat_face:" class="emoji" alt=":cowboy_hat_face:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day06 do
  alias AoC.Input

  def parse(input, _part) do
    stream = Input.stream!(input, trim: true)

    {start, obstacles} =
      stream
      |&gt; Stream.with_index()
      |&gt; Enum.reduce({nil, MapSet.new()}, fn {line, x}, {start, obstacles} -&gt;
        line
        |&gt; String.to_charlist()
        |&gt; Stream.with_index()
        |&gt; Enum.reduce({start, obstacles}, fn
          {?#, y}, {start, obstacles} -&gt; {start, MapSet.put(obstacles, {x, y})}
          {?^, y}, {_start, obstacles} -&gt; {{x, y}, obstacles}
          _, {start, obstacles} -&gt; {start, obstacles}
        end)
      end)

    row_end = Enum.count(stream) - 1
    col_end = length(String.to_charlist(Enum.at(stream, 0))) - 1

    {start, obstacles, row_end, col_end}
  end

  def part_one({{x, y}, obstacles, row_end, col_end}) do
    path = build_path({?^, {x - 1, y}}, obstacles, MapSet.new([{x, y}]), row_end, col_end)
    Enum.count(path)
  end

  def part_two({{x, y}, obstacles, row_end, col_end}) do
    path = build_path({?^, {x - 1, y}}, obstacles, MapSet.new([{x, y}]), row_end, col_end)

    Enum.reduce(path, 0, fn new_obstacle, counter -&gt;
      obstacles = MapSet.put(obstacles, new_obstacle)
      limit = round(row_end * col_end / 2)
      v = check_path({?^, {x - 1, y}}, obstacles, row_end, col_end, 0, limit)
      v + counter
    end)
  end

  defp build_path({direction, {x, y}}, obstacles, solution, row_end, col_end) do
    cond do
      x &lt; 0 or x &gt; row_end or y &lt; 0 or y &gt; col_end -&gt;
        solution

      MapSet.member?(obstacles, {x, y}) -&gt;
        position = direct(direction, {x, y})
        build_path(position, obstacles, solution, row_end, col_end)

      true -&gt;
        position = continue(direction, {x, y})
        build_path(position, obstacles, MapSet.put(solution, {x, y}), row_end, col_end)
    end
  end

  defp check_path({direction, {x, y}}, obstacles, row_end, col_end, counter, limit) do
    cond do
      counter == limit -&gt;
        1

      x &lt; 0 or x &gt; row_end or y &lt; 0 or y &gt; col_end -&gt;
        0

      MapSet.member?(obstacles, {x, y}) -&gt;
        position = direct(direction, {x, y})
        check_path(position, obstacles, row_end, col_end, counter, limit)

      true -&gt;
        position = continue(direction, {x, y})
        check_path(position, obstacles, row_end, col_end, counter + 1, limit)
    end
  end

  defp direct(c, {x, y}) do
    case c do
      ?^ -&gt; {?&gt;, {x + 1, y + 1}}
      ?&gt; -&gt; {?v, {x + 1, y - 1}}
      ?v -&gt; {?&lt;, {x - 1, y - 1}}
      ?&lt; -&gt; {?^, {x - 1, y + 1}}
    end
  end

  defp continue(c, {x, y}) do
    case c do
      ?^ -&gt; {c, {x - 1, y}}
      ?&gt; -&gt; {c, {x, y + 1}}
      ?v -&gt; {c, {x + 1, y}}
      ?&lt; -&gt; {c, {x, y - 1}}
    end
  end
end
</code></pre>
<p>I tried, but I couldn’t figure out when to stop in <code>check_path</code> therefor <code>limit</code> <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">Solution for 2024 day 6
part_one: 4696 in 11.74ms
part_two: 1443 in 1.39s
</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="348766" 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-6/67917/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-348766" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348766"
                     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="348769" data-post-id="348769">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>As promised - here is my code for Day 6. Explanations as to why the string version is so much slower than the map version are welcome. I read the source for <code>String.at</code> and the only explanation I could come up with is that its routine for splitting the string and taking the first char in the second half might be doing two heap allocations to store the left and right halves of the string? I’m not sure if this is true, but it’s the only thing I could come up with.</p>
<p><a href="https://gitea.codingthemsoftly.com/caleb/advent_of_code/src/branch/main/elixir/livebook/2024/day6.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://gitea.codingthemsoftly.com/caleb/advent_of_code/src/branch/main/elixir/livebook/2024/day6.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="348769" 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-6/67917/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-348769" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348769"
                     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="348785" data-post-id="348785">
  <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>this is so fun . first time in advent of code, i have finally hit the inevitable “it will never complete before i go to bed” type of performance <img src="https://forum.elixirforum.com/images/emoji/apple/rofl.png?v=15" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"></p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_6/Part2.ex#L29" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_6/Part2.ex#L29</a></p>
<p>anyone with smarter ways to find “deadlocks” by checking if traversal count is larger than the total length of map ?? <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"> for ALL single characters … <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"></p>
<p>anyway .. its actually taking SECONDS to check each single character <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"> thats so … fun … literally no improvements done on it whatsoever though <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"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348785" 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-6/67917/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-348785" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348785"
                     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="348786" data-post-id="348786">
  <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>im going to throw all my Ryzen 1700 cores on the job … with Tasks … muahaha</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/0/9/09863de4e1e8f6a912828e313259ebfc9acc8b33.png" data-download-href="https://forum.elixirforum.com/uploads/default/09863de4e1e8f6a912828e313259ebfc9acc8b33" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/0/9/09863de4e1e8f6a912828e313259ebfc9acc8b33.png" alt="image" data-base62-sha1="1mfUlQaOdFRuMUAaFvd4zR8kZhx" width="313" height="255"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">313×255 1.99 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><p></p>
<p><img src="https://forum.elixirforum.com/images/emoji/apple/rofl.png?v=15" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"> (PS its just a joke.. im just doing this for fun)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348786" 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-6/67917/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-348786" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348786"
                     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="348790" data-post-id="348790">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I spent much of the time tuning for speed. I’ve got a solution for part 2 that runs in 0.5 seconds on my Macbook Pro M1. One interesting thing I found is that pattern matching in the function arguments on structure properties, is significantly faster than getting them with dot syntax in the function body.</p>
<p>So</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def step(%{obstructions: obstructions, location: location, facing: facing} = _state)) do
</code></pre>
<p>is faster than</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def step(state) do
  ... state.obstructions
  ... state.location
  ... state.facing
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="348790" 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-6/67917/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-348790" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348790"
                     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="348791" data-post-id="348791">
  <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 will eat my hat before i begin thinking hard about performance .. “Need the answer, you do. Seek within, the path will reveal.” .. so tomorrow … <img src="https://forum.elixirforum.com/images/emoji/apple/wave.png?v=15" title=":wave:" class="emoji" alt=":wave:" loading="lazy" width="20" height="20">  … <img src="https://forum.elixirforum.com/images/emoji/apple/laughing.png?v=15" title=":laughing:" class="emoji" alt=":laughing:" 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="348791" 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-6/67917/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-348791" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348791"
                     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="348808" data-post-id="348808">
  <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, not the best, but just under 1 second so I’ll take it. So todays lesson, READ THE DESCRIPTION, again, failed to count the starting position, which was fine for the test case as the path crosses the start position, but that’s not the case for my input data, so was 1 short of the answer.</p>
<p>Would love to get this down some more with anyones helpful suggestions, maybe an ETS table might be a good option as others have mentioned?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    defmodule Aoc2024.Solutions.Y24.Day06 do
      alias AoC.Input

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

      def part_one(problem) do
        matrix = problem |&gt; build_matrix()

        find_start_position(matrix)
        |&gt; move(matrix, :up)
        |&gt; Map.to_list()
        |&gt; Enum.filter(fn {_, v} -&gt; v in ["X", "^"] end)
        |&gt; Enum.count()
      end

      def move(current_position, matrix, direction) do
        next_coord = next_coord(current_position, direction)

        case Map.get(matrix, next_coord) do
          nil -&gt;
            matrix

          square -&gt;
            {next_position, matrix, direction} =
              check_next_position(square, direction, matrix, next_coord, current_position)

            move(next_position, matrix, direction)
        end
      end

      def check_next_position(square, direction, matrix, next_position, _current_position)
          when square in [".", "X", "^"] do
        {next_position, Map.put(matrix, next_position, "X"), direction}
      end

      def check_next_position(_square, direction, matrix, _next_position, current_position) do
        {current_position, matrix, rotate_direction(direction)}
      end

      def find_start_position(matrix) do
        matrix
        |&gt; Enum.to_list()
        |&gt; Enum.find(fn {_, v} -&gt; v == "^" end)
        |&gt; elem(0)
      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 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}
        end
      end

      def rotate_direction(:up), do: :right

      def rotate_direction(:right), do: :down

      def rotate_direction(:down), do: :left

      def rotate_direction(:left), do: :up

      def set_square_visited(matrix, {x, y}) do
        Map.put(matrix, {x, y}, "X")
      end

      def part_two(problem) do
        matrix = problem |&gt; build_matrix()
        start = find_start_position(matrix)

        :persistent_term.put(Matrix, matrix)

        options =
          find_start_position(matrix)
          |&gt; move(matrix, :up)
          |&gt; Map.to_list()
          |&gt; Enum.filter(fn {_, v} -&gt; v == "X" end)

        options
        |&gt; split_into_chunks()
        |&gt; Task.async_stream(fn options -&gt;
          Enum.map(options, fn {{x, y}, _} -&gt;
            # test_matrix = Map.put(matrix, {x, y}, "#")
            move_2(start, :up, MapSet.new(), {x, y})
          end)
        end)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(&amp; &amp;1)
        |&gt; Enum.count()
      end

      defp split_into_chunks(options) do
        workers = :erlang.system_info(:schedulers_online)
        options_count = Enum.count(options)
        options_per_chunk = :erlang.ceil(options_count / workers)

        Enum.chunk_every(options, options_per_chunk)
      end

      defp merge_results_stream(results_stream) do
        Enum.reduce(results_stream, [], fn {:ok, worker_result}, acc -&gt;
          acc ++ worker_result
        end)
      end

      def move_2(current_position, direction, previous, testing_position) do
        if check_been_here_before(current_position, direction, previous) do
          true
        else
          next_coord = next_coord(current_position, direction)
          matrix = :persistent_term.get(Matrix)

          case Map.get(matrix, next_coord) do
            nil -&gt;
              false

            square -&gt;
              {next_position, direction, backtrack} =
                check_next_position_2(
                  square,
                  direction,
                  next_coord,
                  current_position,
                  testing_position
                )

              if backtrack do
                move_2(current_position, direction, previous, testing_position)
              else
                move_2(
                  next_position,
                  direction,
                  MapSet.put(previous, {current_position, direction}),
                  testing_position
                )
              end
          end
        end
      end

      def check_been_here_before(current_position, direction, previous) do
        MapSet.member?(previous, {current_position, direction})
      end

      def check_next_position_2(
            _square,
            direction,
            next_x_y,
            current_position,
            testing_position
          )
          when next_x_y == testing_position do
        {current_position, rotate_direction(direction), true}
      end

      def check_next_position_2(
            square,
            direction,
            next_position,
            _current_position,
            _testing_position
          )
          when square in [".", "X", "^"] do
        {next_position, direction, false}
      end

      def check_next_position_2(
            _square,
            direction,
            _next_position,
            current_position,
            _testing_position
          ) do
        {current_position, rotate_direction(direction), 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="348808" 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-6/67917/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-348808" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348808"
                     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="348811" data-post-id="348811">
  <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>My day 06 solution:</p>
<p><a href="https://github.com/Flo0807/adventofcode/blob/main/2024/06.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/Flo0807/adventofcode/blob/main/2024/06.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="348811" 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-6/67917/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-348811" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348811"
                     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="348825" data-post-id="348825">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My day 6 solution​<img src="https://forum.elixirforum.com/images/emoji/apple/right_arrow_curving_down.png?v=15" title=":right_arrow_curving_down:" class="emoji" alt=":right_arrow_curving_down:" loading="lazy" width="20" height="20"><br>
I was getting wrong counts for the longest time, and finally realized that I had forgotten to account for corners with more than one obstacle; I’d only ever turn the guard once.</p>
<p>I brute-forced it, like most people.  I <em>did</em> use the visited positions from part 1 as obstacle candidates though, and between that and using a <code>MapSet</code> for “seen” spaces, it runs in a little over 4 seconds, which I was happy with.</p>
<p><a href="https://github.com/joelheaps/aoc24/blob/main/day6.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/joelheaps/aoc24/blob/main/day6.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="348825" 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-6/67917/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-348825" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348825"
                     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="348833" data-post-id="348833">
  <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="seeplusplus" data-post="23" data-topic="67917">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seeplusplus/48/32772_2.png" class="avatar"> seeplusplus:</div>
<blockquote>
<p>Explanations as to why the string version is so much slower than the map version are welcome. I read the source for <code>String.at</code> and the only explanation I could come up with is that its routine for splitting the string and taking the first char in the second half might be doing two heap allocations to store the left and right halves of the string?</p>
</blockquote>
</aside>
<p>No, it is the call <code>byte_size_remaining_at(string, position)</code> in <code>do_at/2</code> for calculating the number of bytes to the left of the character to be extracted that makes it slower.</p>
<p>This is calculation is necessary to correctly handle Unicode characters in the string, since the size of each code point varies from one to four bytes. For example, emoji characters are four bytes,  and in order to skip over two emoji characters in the following example, it is necessary to skip over eight bytes:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex&gt; String.at("😀😃😎🥸", 2)
"😎"
</code></pre>
<p>If a string is known to only contain US ASCII characters (as is the case for all text from the Advent of Code web site), the faster <a href="https://www.erlang.org/doc/apps/stdlib/binary#at/2" rel="nofollow"><code>:binary.at/2</code></a> BIF can safely be used instead of <code>String.at/2</code>. That will usually be slightly faster than using a map.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348833" data-batch-url="/posts/batch_likers">
                        5
                      </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-6/67917/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-348833" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348833"
                     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/67917/load_more?page=4">Load more posts (12 remaining)</a>
</div></template></turbo-stream>