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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The last time I needed to deal with a matrix I was able to use <a href="https://ruby-doc.org/stdlib-2.5.1/libdoc/matrix/rdoc/Matrix.html" rel="noopener nofollow ugc">Ruby’s stdlib</a> and I definitely missed it here.</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/tfwright/11513a64d022b6f543045d85ab481661">
  <header class="source">

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

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



  </article>

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

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

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270623" 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/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-270623" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270623"
                     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 #41"></div>
  </section>
</div>
    <div class="postbit" id="270626" data-post-id="270626">
  <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>I use the formatter with elixir-ls in vim, and although it removes trailing spaces from the end of lines in general, it does not do it from within strings. So it must be VS Code, <a class="mention" href="/u/ilosophiep" rel="nofollow">@IloSophiep</a>.</p>
<p>Also, parsing the input is half of the fun. Today was a great opportunity to flex your <code>&lt;&lt;binary&gt;&gt;</code> muscles.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wasn’t at a computer earlier but have now checked this.</p>
<p><a class="mention" href="/u/ilosophiep" rel="nofollow">@IloSophiep</a> It turns out that it isn’t the Elixir formatter, but it is the ElixirLS VS Code extension doing this. There is the VS Code setting <code>Files: Trim Trailing Whitespace</code> which apparently defaults to false, since I have never changed this setting.</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/4/c/4cf6ae5759fa7b823bdb3a7fc4c9bbeab8280664.png" data-download-href="https://forum.elixirforum.com/uploads/default/4cf6ae5759fa7b823bdb3a7fc4c9bbeab8280664" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/4/c/4cf6ae5759fa7b823bdb3a7fc4c9bbeab8280664.png" alt="image" data-base62-sha1="aYQNyU4DqmHj7vaXWc0rueLcj9q" width="690" height="128" data-dominant-color="F7F0DE"><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">807×150 12 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>However, I see that the ElixirLS extension overrides this to true. Since ElixirLS overrides this, VS Code removes the trailing whitespace without regard to semantic meaning.</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/f/d/fd7af9ce243a968b83ce2daf0c1a9c4e822b9b5b.png" data-download-href="https://forum.elixirforum.com/uploads/default/fd7af9ce243a968b83ce2daf0c1a9c4e822b9b5b" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/original/3X/f/d/fd7af9ce243a968b83ce2daf0c1a9c4e822b9b5b.png" alt="image" data-base62-sha1="Aaoh12nk3weEX4hQnnNqOHpX31V" width="690" height="184" data-dominant-color="F1EAD6"><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">803×215 20.3 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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270632" 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-2022-day-5/52258/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-270632" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270632"
                     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 #43"></div>
  </section>
</div>
    <div class="postbit" id="270637" data-post-id="270637">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="adamu" data-post="43" data-topic="52258">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/adamu/48/31482_2.png" class="avatar"> adamu:</div>
<blockquote>
<p>Also, parsing the input is half of the fun. Today was a great opportunity to flex your <code>&lt;&lt;binary&gt;&gt;</code> muscles.</p>
</blockquote>
</aside>
<p>Okay, you got me. I circled back and parsed the stacks, but I grumbled. Lol. Mainly, I wanted to transpose the input, so that’s what I ended up doing.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc """
  List of the initial stacks and their crates
  """
  @spec initial_stacks() :: [stack()]
  def initial_stacks() do
    Utilities.read_data(5, trim: false)
    # Get the stack data only, leaving off the instructions
    |&gt; Enum.take_while(fn s -&gt; s != "\n" end)
    # Chunk every line into "column" elements
    |&gt; Enum.map(fn line -&gt;
      line
      |&gt; String.codepoints()
      |&gt; Enum.chunk_every(4)
      |&gt; Enum.map(&amp;Enum.join/1)
      |&gt; Enum.map(&amp;String.trim/1)
    end)
    # Transpose the lines so as to turn each line into a single stack,
    # with the first element of the list being the top stack element
    |&gt; Utilities.transpose()
    |&gt; Enum.map(fn crates -&gt;
      crates
      # Drop the stack number from the end
      |&gt; Utilities.drop_last()
      # Get rid of the "empty" crates on top of the real crates
      |&gt; Enum.filter(&amp;Utilities.non_empty?/1)
      # Parse the crates to just get the name
      |&gt; Enum.map(fn "[" &lt;&gt; &lt;&lt;crate :: binary-1&gt;&gt; &lt;&gt; "]" -&gt; crate end)
    end)
    |&gt; Enum.map(&amp;Stack.new/1)
  end
</code></pre>
<p>I could probably compress this a little (fusing some maps and filters) but will leave it as is for now. Curious if anyone else did it similarly, though I suppose it’s implicitly done this way no matter what.</p>
<p>This gives:</p>
<p></p><div class="lightbox-wrapper"><a class="lightbox" href="https://forum.elixirforum.com/uploads/default/original/3X/5/c/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c.png" data-download-href="https://forum.elixirforum.com/uploads/default/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c" title="image" rel="nofollow"><img src="https://forum.elixirforum.com/uploads/default/optimized/3X/5/c/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c_2_690x272.png" alt="image" data-base62-sha1="df2hHIiYKRtJse0HxxeLE3qwD7u" width="690" height="272" srcset="https://forum.elixirforum.com/uploads/default/optimized/3X/5/c/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c_2_690x272.png, https://forum.elixirforum.com/uploads/default/optimized/3X/5/c/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c_2_1035x408.png 1.5x, https://forum.elixirforum.com/uploads/default/original/3X/5/c/5ccf6718d8ae99e122f3230a3bf8a3b58f1e122c.png 2x" data-dominant-color="E6E8E9"><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">1367×539 56 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> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you very much! I added this to my <code>settings.json</code></p>
<pre data-code-wrap="json"><code class="lang-json">{
    "[elixir]": {
        "files.trimTrailingWhitespace": false,
    }
}
</code></pre>
<p>and now i can properly write the multiline docs without my whitespaces being removed! Thank you (and <a class="mention" href="/u/stevensonmt" rel="nofollow">@stevensonmt</a> and <a class="mention" href="/u/kwando" rel="nofollow">@kwando</a>)  for the help! <img src="https://forum.elixirforum.com/images/emoji/apple/heart.png?v=15" title=":heart:" class="emoji" alt=":heart:" 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="270651" 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/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-270651" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270651"
                     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 #45"></div>
  </section>
</div>
    <div class="postbit" id="270663" data-post-id="270663">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>late to the party.. I hate my solution already after seeing some of the more elegant ways here <img src="https://forum.elixirforum.com/images/emoji/apple/grimacing.png?v=15" title=":grimacing:" class="emoji" alt=":grimacing:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ExAOC2022.Day5 do
  @input "./lib/day5_input.txt"

  def puzzle1() do
    {initial_state, instructions} = init()

    new_state = Enum.reduce(instructions, initial_state, &amp;run_instructions/2)

    get_top_boxes(new_state)
  end

  def puzzle2() do
    {initial_state, instructions} = init()

    new_state = Enum.reduce(instructions, initial_state, &amp;(run_instructions(&amp;1, &amp;2, true)))

    get_top_boxes(new_state)
  end

  #
  #
  # private shenanigans
  #
  # loads data and sets up initial state
  defp init() do
    [port, _, instructions, _rest] =
      @input
      |&gt; file_by_line()
      |&gt; Enum.chunk_by(&amp;(&amp;1 == ""))

    {parse_state(Enum.reverse(port)), instructions}
  end

  # runs instructions duh
  defp run_instructions(instr, state, batch_process? \\ false) do
    [[times], [from_id], [to_id]] = Regex.scan(~r/\d+/, instr)

    src_stack = state[from_id]
    trgt_stack = state[to_id]

    {updated_trgt, updated_src} = for _i &lt;- 1..String.to_integer(times), reduce: {[], src_stack} do
      {trgt_acc, src_acc} -&gt;
        {box, new_src} = pop(src_acc)
        new_trgt = push(trgt_acc, box)

        {new_trgt, new_src}
    end

    updated_trgt = if batch_process?, do: Enum.reverse(updated_trgt), else: updated_trgt

    state
    |&gt; Map.put(from_id, updated_src)
  #
  #
  # private shenanigans
  #
  # loads data and sets up initial state
  defp init() do
    [port, _, instructions, _rest] =
      @input
      |&gt; file_by_line()
      |&gt; Enum.chunk_by(&amp;(&amp;1 == ""))

    {parse_state(Enum.reverse(port)), instructions}
  end

  # runs instructions duh
  defp run_instructions(instr, state, batch_process? \\ false) do
    [[times], [from_id], [to_id]] = Regex.scan(~r/\d+/, instr)

    src_stack = state[from_id]
    trgt_stack = state[to_id]

    {updated_trgt, updated_src} = for _i &lt;- 1..String.to_integer(times), reduce: {[], src_stack} do
      {trgt_acc, src_acc} -&gt;
        {box, new_src} = pop(src_acc)
        new_trgt = push(trgt_acc, box)

        {new_trgt, new_src}
    end

    updated_trgt = if batch_process?, do: Enum.reverse(updated_trgt), else: updated_trgt

    state
    |&gt; Map.put(from_id, updated_src)
    |&gt; Map.put(to_id, updated_trgt ++ trgt_stack)
  end

  # returns top box from each stack
  defp get_top_boxes(state) do
    for {_, stack} &lt;- state do
      {top_item, _} = pop(stack)
      top_item
    end
    |&gt; Enum.join
  end

  defp parse_state([head | rest]) do
    initial_map = create_map(head)

    rest
    |&gt; Enum.reduce(initial_map, fn row, state -&gt;
      parsed_row = parse_row(row)

      for {val, stack_id} &lt;- parsed_row, reduce: state do
        acc -&gt;
          if val !== "", do: Map.update(acc, "#{stack_id}", [], fn stack -&gt; push(stack, val) end), else: acc
      end
    end)
  end

  # turns a state row into something usable
  defp parse_row(row) do
    row
    |&gt; String.graphemes
    |&gt; Enum.chunk_every(4)
    |&gt; Enum.map(&amp;extract_box_id/1)
    |&gt; Enum.with_index(1)
  end

  defp extract_box_id(box_info), do: box_info |&gt; Enum.join() |&gt; String.replace(~r/[^A-Z]/, "")

  # initializes the state map based on the input data
  defp create_map(head) do
    head
    |&gt; String.trim()
    |&gt; String.split()
    |&gt; Map.from_keys([])
  end

  # stack stack stack
  defp push(stack, item) do
    [item | stack]
  end

  defp pop([item | rest]) do
    {item, rest}
  end

  defp file_by_line(file) do
    file
    |&gt; File.read!()
    |&gt; String.split(~r/\R/)
  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="270663" 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/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-270663" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270663"
                     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 #46"></div>
  </section>
</div>
    <div class="postbit" id="270664" data-post-id="270664">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So good!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270664" 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/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-270664" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270664"
                     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 #47"></div>
  </section>
</div>
    <div class="postbit" id="270812" data-post-id="270812">
  <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>Parsing</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[stack, moves] =
  input
  |&gt; String.split(~r/\n\n/)

[_ | stack] =
  stack
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.reverse()

stack =
  stack
  |&gt; Enum.map(&amp;String.codepoints/1)
  |&gt; Enum.zip_with(&amp; &amp;1)
  |&gt; Enum.reject(fn
    ["[" | _] -&gt; true
    ["]" | _] -&gt; true
    [" " | _] -&gt; true
    _ -&gt; false
  end)
  |&gt; Enum.map(fn row -&gt;
    row |&gt; Enum.reverse() |&gt; Enum.reject(fn crate -&gt; crate == " " end)
  end)
  |&gt; Enum.with_index(fn stack, i -&gt; {i + 1, stack} end)
  |&gt; Enum.into(%{})

moves =
  moves
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.map(fn command -&gt;
    %{"amount" =&gt; amount, "from" =&gt; from, "to" =&gt; to} =
      Regex.named_captures(~r/move (?&lt;amount&gt;\d+) from (?&lt;from&gt;\d+) to (?&lt;to&gt;\d+)/, command)

    %{
      amount: String.to_integer(amount),
      from: String.to_integer(from),
      to: String.to_integer(to)
    }
  end)
</code></pre>
<p>Part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">moves
|&gt; Enum.reduce(stack, fn move, acc -&gt;
  {crates, rest} = Map.get(acc, move.from) |&gt; Enum.split(move.amount)
  acc
  |&gt; Map.put(move.from, rest)
  |&gt; Map.update(move.to, [], &amp; Enum.reverse(crates) ++ &amp;1)
end)
|&gt; Enum.to_list()
|&gt; Enum.sort_by(&amp; elem(&amp;1, 0))
|&gt; Enum.map(fn {_, [first | _]} -&gt; first end)
|&gt; Enum.join()
</code></pre>
<p>Part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">moves
|&gt; Enum.reduce(stack, fn move, acc -&gt;
  {crates, rest} = Map.get(acc, move.from) |&gt; Enum.split(move.amount)
  acc
  |&gt; Map.put(move.from, rest)
  |&gt; Map.update(move.to, [], &amp; crates ++ &amp;1)
end)
|&gt; Enum.to_list()
|&gt; Enum.sort_by(&amp; elem(&amp;1, 0))
|&gt; Enum.map(fn {_, [first | _]} -&gt; first end)
|&gt; Enum.join()
</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="270812" 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/49">Post #48</a>
	                </div>
	            </div>
              <div id="likers-container-270812" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270812"
                     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 #48"></div>
  </section>
</div>
    <div class="postbit" id="271179" data-post-id="271179">
  <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>Phew… this one was hard for me. I’m not really used to functional programming, so what I would normally do in a stateful object-oriented programming language didn’t work here. Anyway, here’s my solution:</p>
<p><a href="https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day5.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day5.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="271179" 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/50">Post #49</a>
	                </div>
	            </div>
              <div id="likers-container-271179" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="271179"
                     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 #49"></div>
  </section>
</div>
    <div class="postbit" id="271260" data-post-id="271260">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Input parsing was a struggle, but I found that that Enum.reduce was a very nice way of handling it once it got in. I had written out a first solution that took each part separately, but I realized there was some code duplication that only differed by the reducing function, so I refactored it out as shared code with a function parameter, that I quite like. Still feel that I could simplify some of the pipe commands to be cleaner/better aligned.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day5 do

  def debugger(thing) do
    IO.puts(thing)
    thing
  end

  # Read the file

  def getInput() do
    File.read!("inputs/day-5-input.txt")
    |&gt; String.split("\n\n")
    |&gt; parseInput
  end

  def parseInput([state, moves]), do: {parseState(state), parseMoves(moves)}

  def parseState(bundle) do
    bundle
    |&gt; String.replace(~r"[\[\]]", " ")
    |&gt; String.split("\n")
    |&gt; Enum.map(fn line -&gt;
      line
      |&gt; String.codepoints
      |&gt; Enum.chunk_every(4)
      |&gt; Enum.map(fn [_, char | _rest] -&gt;
        String.trim(char)
      end)
    end)
    |&gt; List.zip
    |&gt; Enum.map(fn col -&gt;
      Tuple.to_list(col) |&gt; Enum.filter(&amp;("" != &amp;1)) |&gt; List.pop_at(-1)
    end)
    |&gt; Map.new
  end

  def parseMoves(bundle) do
    bundle
    |&gt; String.split("\n")
    |&gt; Enum.map(fn line -&gt;
      ["move", count, "from", from, "to", to] = String.split(line, " ")
      { String.to_integer(count), from, to }
    end)
  end

  # Part 1
  def single_move(state, 0, _from, _to), do: state
  def single_move(state, count, from, to) do
    [crate | rest] = state[from]
    bottom = state[to]
    state
    |&gt; Map.put(from, rest)
    |&gt; Map.put(to, [crate | bottom])
    |&gt; single_move(count - 1, from, to)
  end

  # Part 2
  def multi_move(state, count, from, to) do
    {crates, rest} = Enum.split(state[from], count)
    bottom = state[to]
    state
    |&gt; Map.put(from, rest)
    |&gt; Map.put(to, crates ++ bottom)
  end

  # Shared

  def sequnce_moves(reducer) do
    {state, moves} = getInput()
    Enum.reduce(moves, state, fn {count, from, to}, state -&gt;
      reducer.(state, count, from, to)
    end)
    |&gt; getTop
    |&gt; Enum.join("")
  end

  def getTop(state) do
    for i &lt;- ?1..?9, do: state |&gt; Map.get(&lt;&lt;i&gt;&gt;) |&gt; List.first
  end

end

Advent.Day5.sequnce_moves(&amp;Advent.Day5.single_move/4) |&gt; IO.puts

Advent.Day5.sequnce_moves(&amp;Advent.Day5.multi_move/4) |&gt; IO.puts
</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="271260" 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/51">Post #50</a>
	                </div>
	            </div>
              <div id="likers-container-271260" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="271260"
                     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 #50"></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=6">Load more posts (1 remaining)</a>
</div></template></turbo-stream>