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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="deadbeef" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  deadbeef
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Similar approach to others. Think I just organized parsing/solving a bit different.<br>
I end up committing regex for text splitting, but commented non-regex options (e.g. patterned matched the moves originally)</p>
<p><a href="https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d05.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d05.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="270513" 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-5/52258/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-270513" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270513"
                     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="270515" data-post-id="270515">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="deadbeef" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  deadbeef
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wondered the same thing about simplifying peek/drop/push.</p>
<details>
<summary>
I ended up doing something like this...</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">crates = # current state: %{column_idx =&gt; [crate]}
[count, from, to] = # parsed: "move &lt;count&gt; from &lt;from&gt; to &lt;to&gt;"
{to_move, crates} = Map.get_and_update(crates, from, &amp;Enum.split(&amp;1, count))
# Reverse is for part 1 only, remove for part 2
Map.update(crates, to, [], &amp;(Enum.reverse(to_move) ++ &amp;1))
</code></pre>
</details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270515" 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-5/52258/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-270515" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270515"
                     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="270525" data-post-id="270525">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Lenses in general aren’t really popular outside of the “hard core FP”.</p>
<p>My solution:</p>
<p><a href="https://github.com/hauleth/advent-of-code/blob/master/2022/day05.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/hauleth/advent-of-code/blob/master/2022/day05.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="270525" 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-5/52258/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-270525" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270525"
                     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="270530" data-post-id="270530">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I didn’t enjoy the parsing process.  Here’s my code for it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def read_input(fname) do
    [crates, moves] =
      File.read!("priv/#{fname}")
      |&gt; String.split("\n\n", trim: true)

    {to_map(crates), to_cmdlist(moves)}
  end

  # input -&gt; list of rows
  # each row:
  # - split into list of single-character strings
  # - drop the [ and ] characters
  # - filter out "empty" crates
  # - add index to turn it into {crate, column} tuples
  # use flatmap to turn it into a map
  # - the key is the column (1, 2, ...)
  # - value is a list of crates in that column 
  defp to_map(crates) do
    String.split(crates, "\n", trim: true)
    |&gt; Enum.flat_map(fn row -&gt;
      String.graphemes(row)
      |&gt; Enum.drop_every(2)
      |&gt; Enum.with_index()
      |&gt; Enum.filter(fn {crate, _} -&gt; crate != " " end)
    end)
    |&gt; Enum.reverse()
    |&gt; Enum.reduce(%{}, fn {crate, col}, cratesmap -&gt;
      Map.update(cratesmap, div(col, 2) + 1, [crate], fn existing -&gt; [crate | existing] end)
    end)
  end

  defp to_cmdlist(moves) do
    moves
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(fn movestr -&gt;
      [_, count, _, from, _, to] = String.split(movestr, " ", trim: true)
      {String.to_integer(count), String.to_integer(from), String.to_integer(to)}
    end)
  end

</code></pre>
<p>Moving the crates was more work than necessary:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def part1({crates, commands}) do
    Enum.reduce(commands, crates, fn {count, from, to}, crates -&gt;
      {take, remain} = Enum.split(crates[from], count)

      Map.put(crates, from, remain)
      |&gt; Map.put(to, Enum.reverse(take) ++ crates[to])
    end)
    |&gt; top_crates()
  end

  def part2({crates, commands}) do
    Enum.reduce(commands, crates, fn {count, from, to}, crates -&gt;
      {take, remain} = Enum.split(crates[from], count)

      Map.put(crates, from, remain)
      |&gt; Map.put(to, take ++ crates[to])
    end)
    |&gt; top_crates()
  end

  defp top_crates(crates) do
    Map.keys(crates)
    |&gt; Enum.sort()
    |&gt; Enum.map(fn col -&gt; hd(crates[col]) end)
    |&gt; Enum.join("")
  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="270530" 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-5/52258/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-270530" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270530"
                     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="270531" data-post-id="270531">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I spent much more time parsing the input than moving the crates <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>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day05 do
  def part1(input_path) do
    {stacks, instructions} = parse_input(input_path)

    instructions
    |&gt; Enum.reduce(stacks, fn instruction, stacks -&gt;
      move1(stacks, instruction.from, instruction.to, instruction.amount)
    end)
    |&gt; Enum.sort()
    |&gt; Enum.map(&amp;elem(&amp;1, 1))
    |&gt; Enum.map(&amp;hd/1)
  end

  def part2(input_path) do
    {stacks, instructions} = parse_input(input_path)

    instructions
    |&gt; Enum.reduce(stacks, fn instruction, stacks -&gt;
      move2(stacks, instruction.from, instruction.to, instruction.amount)
    end)
    |&gt; Enum.sort()
    |&gt; Enum.map(&amp;elem(&amp;1, 1))
    |&gt; Enum.map(&amp;hd/1)
  end

  defp parse_input(input_path) do
    [stacks_part, instructions_part] =
      input_path
      |&gt; File.read!()
      |&gt; String.split("\n\n", parts: 2, trim: true)

    {build_stacks(stacks_part), build_instructions(instructions_part)}
  end

  defp build_stacks(stacks_part) do
    stacks_part
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.reverse()
    |&gt; tl()
    |&gt; Enum.map(&amp;to_layer(&amp;1, []))
    |&gt; Enum.reduce(%{}, &amp;push_layer/2)
  end

  defp to_layer("", acc), do: Enum.reverse(acc)
  defp to_layer("    " &lt;&gt; rest, acc), do: to_layer(rest, [?_ | acc])
  defp to_layer("   " &lt;&gt; rest, acc), do: to_layer(rest, [?_ | acc])
  defp to_layer(&lt;&lt;?[, char, ?], ?\s, rest::binary&gt;&gt;, acc), do: to_layer(rest, [char | acc])
  defp to_layer(&lt;&lt;?[, char, ?], rest::binary&gt;&gt;, acc), do: to_layer(rest, [char | acc])

  defp push_layer(layer, stacks) do
    layer
    |&gt; Enum.with_index(1)
    |&gt; Enum.reduce(stacks, fn
      {?_, _}, stacks -&gt; stacks
      {char, i}, stacks -&gt; Map.update(stacks, i, [char], &amp;[char | &amp;1])
    end)
  end

  defp build_instructions(instructions_part) do
    ~r/\d+/m
    |&gt; Regex.scan(instructions_part)
    |&gt; List.flatten()
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; Enum.chunk_every(3)
    |&gt; Enum.map(fn [amount, from, to] -&gt;
      %{amount: amount, from: from, to: to}
    end)
  end

  defp move1(stacks, from, to, amount) do
    {from_stack, to_stack} = for _ &lt;- 1..amount, reduce: {stacks[from], stacks[to]} do
      {[h | t], to_stack} -&gt; {t, [h | to_stack]}
    end

    %{stacks | from =&gt; from_stack, to =&gt; to_stack}
  end

  defp move2(stacks, from, to, amount) do
    {to_move, from_stack} = Enum.split(stacks[from], amount)
    to_stack = to_move ++ stacks[to]

    %{stacks | from =&gt; from_stack, to =&gt; to_stack}
  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="270531" 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-5/52258/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-270531" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270531"
                     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="270533" data-post-id="270533">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I certainly overengieneered this, as I created a struct for holding the state and having an API to call, transforming from and to text (for debugging). But that also meant part 2 was actually just taking away from the existing solution <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>
<details>
<summary>
Solution</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day5 do
  defmodule State do
    defstruct stacks: %{}

    @col_size 3

    def from_text(text) do
      lines = String.split(text, "\n")
      {labels_line, stack_lines} = List.pop_at(lines, -1)
      labels = parse_columns(" ", labels_line)
      stack_columns = Enum.map(stack_lines, &amp;parse_columns([" ", "[", "]"], &amp;1))

      stacks =
        stack_columns
        |&gt; Enum.reverse()
        |&gt; Enum.reduce(Enum.map(labels, &amp;{&amp;1, []}), fn line, stack -&gt;
          Enum.zip(stack, line)
          |&gt; Enum.map(fn
            {col, ""} -&gt; col
            {{label, stack}, cargo} -&gt; {label, [cargo | stack]}
          end)
        end)

      %__MODULE__{stacks: Map.new(stacks)}
    end

    defp parse_columns(trim, &lt;&lt;text::binary-size(@col_size)&gt;&gt;) do
      [trim(text, trim)]
    end

    defp parse_columns(trim, &lt;&lt;text::binary-size(@col_size), " ", rest::binary&gt;&gt;) do
      [trim(text, trim) | parse_columns(trim, rest)]
    end

    defp trim(string, to_trim) do
      to_trim
      |&gt; List.wrap()
      |&gt; Enum.reduce(string, &amp;String.trim(&amp;2, &amp;1))
    end

    def move(%__MODULE__{stacks: stacks}, from, to, num) do
      {to_move, stacks} =
        Map.get_and_update!(stacks, from, fn list -&gt;
          Enum.split(list, num)
        end)

      to_put = Enum.reverse(to_move)

      stacks = Map.update!(stacks, to, fn current -&gt; to_put ++ current end)
      %__MODULE__{stacks: stacks}
    end

    def move_alternate(%__MODULE__{stacks: stacks}, from, to, num) do
      {to_move, stacks} =
        Map.get_and_update!(stacks, from, fn list -&gt;
          Enum.split(list, num)
        end)

      stacks = Map.update!(stacks, to, fn current -&gt; to_move ++ current end)
      %__MODULE__{stacks: stacks}
    end

    def id(%__MODULE__{stacks: stacks}) do
      stacks
      |&gt; Map.values()
      |&gt; Enum.map_join("", &amp;List.first/1)
    end

    defimpl Inspect do
      def inspect(%_{stacks: stacks}, _) do
        stack_lists = Map.values(stacks)
        height = stack_lists |&gt; Enum.map(&amp;length/1) |&gt; Enum.max()
        indexes = -height..-1//1

        lines =
          Enum.map_join(indexes, "\n", fn index -&gt;
            Enum.map_join(stack_lists, " ", fn stack -&gt;
              case Enum.at(stack, index) do
                nil -&gt; "   "
                cargo -&gt; "[#{cargo}]"
              end
            end)
          end)

        labels = stacks |&gt; Map.keys() |&gt; Enum.map_join(" ", fn label -&gt; " #{label} " end)

        text = lines &lt;&gt; "\n" &lt;&gt; labels

        """
        Day5.State.from_text(\"\"\"
        #{text}
        \"\"\")
        """
      end
    end
  end

  def parse(text) do
    [starting_stacks, procedure] = String.split(text, "\n\n", parts: 2)
    state = State.from_text(starting_stacks)

    operations =
      ~r/^move (\d+) from (.) to (.)$/m
      |&gt; Regex.scan(procedure)
      |&gt; Enum.map(fn [_, num, from, to] -&gt;
        %{num: String.to_integer(num), from: from, to: to}
      end)

    {state, operations}
  end

  def run_operations(text) do
    {state, operations} = parse(text)

    operations
    |&gt; Enum.reduce(state, fn operation, state -&gt;
      State.move(state, operation.from, operation.to, operation.num)
    end)
    |&gt; State.id()
  end

  def run_operations_alternate(text) do
    {state, operations} = parse(text)

    operations
    |&gt; Enum.reduce(state, fn operation, state -&gt;
      State.move_alternate(state, operation.from, operation.to, operation.num)
    end)
    |&gt; State.id()
  end
end
</code></pre>
</details> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Getting harder. I’m using Advent of Code to get more familiar with Elixir and functional language and until now it was very pleasant thanks to Elixir amazing stdlib.</p>
<p>But I’m hitting the conceptual wall today. An operation thas is trivial in many languages (pop a few item from a container, put them in another) became incredibly difficult with Elixir and its immutable structure. I spent hours trying to figure out how to do that, only to end up with a bunch of hard to understand Enum.reduce.<br>
Going to study the other answers in this thread for sure!</p>
<details>
<summary>
Day5</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">the_crates = [
  ["P", "Z", "M", "T", "R", "C", "N"],
  ["Z", "B", "S", "T", "N", "D"],
  ["G", "T", "C", "F", "R", "Q", "H", "M"],
  ["Z", "R", "G"],
  ["H", "R", "N", "Z"],
  ["D", "L", "Z", "P", "W", "S", "H", "F"],
  ["M", "G", "C", "R", "Z", "D", "W"],
  ["Q", "Z", "W", "H", "L", "F", "J", "S"],
  ["N", "W", "P", "Q", "S"]
]

defmodule Day5 do
  def move_crates(crates, quantity, from, to) do
    from = from - 1
    to = to - 1
    {crates_to_move, remaining} = Enum.at(crates, from)  |&gt; Enum.split(quantity)
    crates
    |&gt; Enum.with_index
    |&gt; Enum.reduce([], fn {stack, index}, acc -&gt;
      case index do
        ^from -&gt; acc ++ [remaining]
        # part 1
        ^to -&gt; acc ++ [Enum.concat(Enum.reverse(crates_to_move), stack)]
        # part 2
        #^to -&gt; acc ++ [Enum.concat(crates_to_move, stack)]
        _ -&gt; acc ++ [stack]
       end
    end)
  end
end


File.stream!("input05")
|&gt; Stream.map(fn line -&gt;
     String.split(line, ["move","from"," to"], trim: true)
     |&gt; Enum.map(&amp;String.trim/1)
     |&gt; Enum.map(&amp;String.to_integer/1)
   end)
|&gt; Enum.reduce(the_crates, fn [quantity, from, to], acc -&gt;
     Day5.move_crates(acc, quantity, from, to)
    end)
|&gt; Enum.map(&amp;hd/1)
|&gt; Enum.join("")
|&gt; IO.puts
</code></pre>
</details> 
	            </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hst337" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hst337
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>One more linear binary scanning day</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC do

  import :erlang, only: [setelement: 3, element: 2]

  def run(input) do
    {towers, commands_str} = traverse_towers(input)
    IO.inspect(towers)
    towers = traverse_commands(commands_str, towers)
    to_string Enum.map(Tuple.to_list(towers), fn [head | _] -&gt; head end)
  end

  defp index_for(?\s, index), do: index + 1
  defp index_for(?\n, index), do: 1

  def traverse_towers(string, index \\ 1, towers \\ {}) do
    case string do
      &lt;&lt;"\nmove ", tail :: binary&gt;&gt; -&gt;
        {towers, tail}

      &lt;&lt;"   ", delimiter, tail :: binary&gt;&gt; -&gt;
        towers =
          with towers when tuple_size(towers) &lt; index &lt;- towers do
            Tuple.append(towers, [])
          end
        traverse_towers(tail, index_for(delimiter, index), towers)

      &lt;&lt;?[, char, ?], delimiter, tail :: binary&gt;&gt; -&gt;
        towers =
          case towers do
            towers when tuple_size(towers) &lt; index -&gt;
              Tuple.append(towers, [char])

            _ -&gt;
              tower = element(index, towers)
              setelement(index, towers, [char | tower])
          end

        traverse_towers(tail, index_for(delimiter, index), towers)

      &lt;&lt;" ", index_char, " ", _, tail :: binary&gt;&gt; when index_char in ?0..?9 -&gt;
        tower = element(index, towers)
        tower = :lists.reverse(tower)
        towers = setelement(index, towers, tower)
        traverse_towers(tail, index + 1, towers)
    end
  end

  defp traverse_commands(string, towers, number \\ 0, acc \\ [])
  defp traverse_commands("", towers, 0, [to, from, how_much]) do
    move(towers, how_much, from, to)
  end
  defp traverse_commands(string, towers, 0, [to, from, how_much] = acc) do
    towers = move(towers, how_much, from, to)
    traverse_commands(string, towers, 0, [])
  end
  defp traverse_commands(string, towers, number, acc) do
    case string do
      &lt;&lt;"\nmove ", tail :: binary&gt;&gt; -&gt;
        traverse_commands(tail, towers, 0, [number | acc])

      &lt;&lt;" from ", tail :: binary&gt;&gt; -&gt;
        traverse_commands(tail, towers, 0, [number | acc])

      &lt;&lt;" to ", tail :: binary&gt;&gt; -&gt;
        traverse_commands(tail, towers, 0, [number | acc])

      "\n" -&gt;
        traverse_commands("", towers, 0, [number | acc])

      &lt;&lt;char, tail :: binary&gt;&gt; -&gt;
        # IO.inspect &lt;&lt;char&gt;&gt;, label: :char
        traverse_commands(tail, towers, number * 10 + char - ?0, acc)

      "" -&gt;
        traverse_commands("", towers, 0, [number | acc])
    end
  end

  defp move(towers, how_much, from_index, to_index) do
    to = element(to_index, towers)
    from = element(from_index, towers)
    {moving, from} = :lists..split(how_much, from)
    to = moving ++ to

    towers = setelement(to_index, towers, to)
    setelement(from_index, towers, from)
  end

end

IO.inspect AOC.run IO.read :eof
</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="270538" 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-5/52258/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-270538" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270538"
                     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="270539" data-post-id="270539">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hst337" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hst337
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And it is also extremely unobvious and unsupported feature. Performance, structures, lenses as lists, introspection, <code>Access.key</code> etc.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day5 do
  def input do
    [stacks, instructions] = File.read!("input5.txt") |&gt; String.split("\n\n", trim: true)

    stacks = map_stacks(stacks)
    instructions = map_instructions(instructions)

    [stacks, instructions]
  end

  def part1 do
    [stacks, instructions] = input()

    Enum.reduce(instructions, stacks, fn [count, from, to] , acc -&gt;
      to_move = Enum.at(acc, from - 1) |&gt; Enum.take(count) |&gt; Enum.reverse()

      List.update_at(acc, from - 1, fn x -&gt; Enum.drop(x, count) end)
      |&gt; List.update_at(to - 1, fn x -&gt; to_move ++ x end)
    end)
    |&gt; Enum.map(&amp;List.first/1)
    |&gt; Enum.join("")
  end

  def part2 do
    [stacks, instructions] = input()

    Enum.reduce(instructions, stacks, fn [count, from, to] , acc -&gt;
      to_move = Enum.at(acc, from - 1) |&gt; Enum.take(count)

      List.update_at(acc, from - 1, fn x -&gt; Enum.drop(x, count) end)
      |&gt; List.update_at(to - 1, fn x -&gt; to_move ++ x end)
    end)
    |&gt; Enum.map(&amp;List.first/1)
    |&gt; Enum.join("")
  end

  @doc """
    Maps the given rows of stacks to columns, and removes the numbers

    ## Example

      iex&gt; Day5.map_stacks("   [D]    \\n[N] [C]    \\n[Z] [M] [P]\\n 1   2   3 ")
      [["N", "Z"], ["D", "C", "M"], ["P"]]
  """
  def map_stacks(stacks) do
    stacks
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(fn row -&gt;
      row
      |&gt; String.split("", trim: true)
      |&gt; Enum.chunk_every(4)
      |&gt; Enum.map(fn item -&gt; Enum.take(item, 3) |&gt; Enum.at(1) end)
    end)
    |&gt; Enum.reverse()
    |&gt; Enum.drop(1)
    |&gt; Enum.reverse()
    |&gt; Enum.zip_with(&amp; &amp;1)
    |&gt; Enum.map(&amp;Enum.reject(&amp;1, fn x -&gt; x == " " end))
  end

  @doc """
    Maps the instructions to a list of numbers [count, from, to]

    ## Example

      iex&gt; Day5.map_instructions("move 1 from 2 to 1\\nmove 3 from 1 to 3\\nmove 2 from 2 to 1\\nmove 1 from 1 to 2\\n")
      [[1, 2, 1], [3, 1, 3], [2, 2, 1], [1, 1, 2]]
  """
  def map_instructions(instructions) do
    instructions
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(fn instruction -&gt;
      instruction
      |&gt; String.trim_leading("move ")
      |&gt; String.replace("from ", "")
      |&gt; String.replace("to ", "")
      |&gt; String.split(" ")
      |&gt; Enum.map(&amp;String.to_integer/1)
    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="270544" 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-5/52258/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-270544" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270544"
                     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/52258/load_more?page=3">Load more posts</a>
</div></template></turbo-stream>