<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="270897" data-post-id="270897">
  <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>Again a bit late to the party and probably more complicated than needed to be, but it feels pretty readable to me. I also had great fun exploring the Path module. That is, until I had a bug changing into parent folders that had me puzzled for easily an hour <img src="https://forum.elixirforum.com/images/emoji/apple/see_no_evil_monkey.png?v=15" title=":see_no_evil_monkey:" class="emoji" alt=":see_no_evil_monkey:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ExAOC2022.Day7 do

  @input "./lib/day7_input.txt"

  @disk_size 70_000_000
  @required_space 30_000_000

  def puzzle1() do
    {_path, sizes} = analyze_file_system()

    Enum.reduce(sizes, 0, fn {_, v}, acc -&gt; if v &lt;= 100_000, do: acc + v, else: acc end)
  end

  def puzzle2() do
    {_path, sizes} = analyze_file_system()

    space_left = @disk_size - sizes["/"]
    space_needed = @required_space - space_left

    Map.values(sizes) |&gt; Enum.sort |&gt; Enum.find(&amp;(&amp;1 &gt;= space_needed))
  end

  defp analyze_file_system() do
    @input
    |&gt; file_by_line
    |&gt; Enum.map(&amp;String.split/1)
    |&gt; Enum.reduce({nil, %{}}, &amp;run/2)
  end

  defp run(["$", "cd", "/"], {_, sizes}), do: {Path.rootname("/"), Map.put(sizes, "/", 0)}
  defp run(["$", "cd", ".."], {path, sizes}), do: {parent_dir(path), sizes}
  defp run(["$", "cd", dir], {path, sizes}) do
    path = path |&gt; Path.join(dir)
    {path, Map.put(sizes, path, 0)}
  end
  defp run(["$", _], acc), do: acc
  defp run(["dir", _], acc), do: acc
  defp run([size, _file], {path, sizes}) do
    sizes = for {k, _v} &lt;- sizes, reduce: sizes do
      acc -&gt;
        if String.contains?(path, k) do
          Map.update!(acc, k, &amp;(&amp;1 + String.to_integer(size)))
        else
          acc
        end
    end
    {path, sizes}
  end

  defp run(_, acc), do: acc

  defp parent_dir(path) do
    path
    |&gt; Path.split()
    |&gt; Enum.reverse()
    |&gt; tl()
    |&gt; Enum.reverse
    |&gt; Path.join()
  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="270897" 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-7/52324/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-270897" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270897"
                     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="270898" data-post-id="270898">
  <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>Wow, I like your solution a lot! Very cool</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270898" 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-7/52324/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-270898" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270898"
                     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="270899" data-post-id="270899">
  <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>Did not like today’s puzzle. Connection to elves more tenuous than usual and I still don’t follow the logic of how these sizes are being calculated. Had to grit my teeth and push through the second half when I realized the outer directory total wasn’t complete because the commands never back out of the final path <img src="https://forum.elixirforum.com/images/emoji/apple/angry.png?v=15" title=":angry:" class="emoji" alt=":angry:" loading="lazy" width="20" height="20"></p>
<p>use of <code>then/1</code> was basically pure spite at that point</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/tfwright/ddf84ef7f40fea3a95019e4515d0ccd8">
  <header class="source">

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

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/tfwright/ddf84ef7f40fea3a95019e4515d0ccd8" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/tfwright/ddf84ef7f40fea3a95019e4515d0ccd8</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="270899" 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-7/52324/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-270899" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270899"
                     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="271080" data-post-id="271080">
  <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>Parse</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">drive =
  input
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.reduce([], fn
    "$ cd " &lt;&gt; dir, acc -&gt;
      [{:cd, dir} | acc]

    "$ ls", acc -&gt;
      acc

    "dir " &lt;&gt; dir, acc -&gt;
      [{:dir, dir} | acc]

    cmd, acc -&gt;
      [size, name] = String.split(cmd, " ")
      [{:file, String.to_integer(size), name} | acc]
  end)
  |&gt; Enum.reverse()
  |&gt; Enum.reduce({%{}, []}, fn
    {:cd, ".."}, {tree, [_x | new_path]} -&gt;
      {tree, new_path}

    {:cd, dir}, {tree, path} -&gt;
      canonical_path = [dir | path] |&gt; Enum.reverse()
      {Map.put_new(tree, canonical_path, []), [dir | path]}

    {:dir, dir}, {tree, path} -&gt;
      canonical_path = path |&gt; Enum.reverse()
      {Map.update!(tree, canonical_path, &amp;[{:dir, dir} | &amp;1]), path}

    {:file, _size, _name} = file, {tree, path} -&gt;
      canonical_path = path |&gt; Enum.reverse()
      {Map.update!(tree, canonical_path, &amp;[file | &amp;1]), path}

    _, acc -&gt;
      acc
  end)
  |&gt; then(fn {tree, _} -&gt; tree end)
</code></pre>
<p>Drive module</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Drive do
  def dirs_with_size(drive) do
    drive
    |&gt; Enum.reduce(%{}, fn {path, _dir}, acc -&gt;
      Map.put(acc, path, dir_size(path, drive))
    end)
  end

  defp dir_size(path, drive) do
    Map.get(drive, path)
    |&gt; Enum.reduce(0, fn
      {:file, size, _}, acc -&gt; acc + size
      {:dir, dir}, acc -&gt; acc + dir_size(path ++ [dir], drive)
    end)
  end
end
</code></pre>
<p>Part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Drive.dirs_with_size(drive)
|&gt; Enum.reduce(0, fn
  {_, size}, acc when size &lt;= 100_000 -&gt; acc + size
  _, acc -&gt; acc
end)
</code></pre>
<p>Part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">dirs_with_size = Drive.dirs_with_size(drive)
remaining_space = 70_000_000 - dirs_with_size[["/"]]
needed_space = 30_000_000 - remaining_space

dirs_with_size
|&gt; Enum.map(&amp; elem(&amp;1, 1))
|&gt; Enum.sort(:desc)
|&gt; Enum.reduce(fn size, acc -&gt;
  cond do
    size &lt;= acc &amp;&amp; size &gt;= needed_space -&gt; size
    true -&gt; acc
  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="271080" 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-7/52324/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-271080" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="271080"
                     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="271685" data-post-id="271685">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This one broke me. I’m still playing with it. For the test input it worked fine but for the challenge itself, Nah <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule DaySeven do
  defstruct parent_dirs: [], current_dir: "/", dirs: %{"/" =&gt; 0}

  def get_total_size_of_directories_at_most_100k(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.reduce(%DaySeven{}, &amp;process_line/2)
    |&gt; then(fn %DaySeven{dirs: dirs} -&gt; dirs |&gt; Map.values() end)
    |&gt; Enum.filter(&amp;is_size_less_than_100k?/1)
    |&gt; Enum.sum()
  end

  defp process_line("$ cd /", acc), do: acc
  defp process_line("$ ls", acc), do: acc
  defp process_line("dir " &lt;&gt; _rest, acc), do: acc

  defp process_line("$ cd ..", %DaySeven{parent_dirs: [latest_parent | remaining_parents]} = acc) do
    %{acc | current_dir: latest_parent, parent_dirs: remaining_parents}
  end

  defp process_line("$ cd " &lt;&gt; dir_name, %DaySeven{current_dir: new_parent_dir} = acc) do
    updated_dirs = Map.put_new(acc.dirs, dir_name, 0)
    %{acc | current_dir: dir_name, parent_dirs: [new_parent_dir | acc.parent_dirs], dirs: updated_dirs}
  end

  defp process_line(file_line, acc) do
    [size | _] =
      file_line
      |&gt; String.split(" ", trim: true)

    size
    |&gt; Integer.parse()
    |&gt; process_file_size(acc)
  end

  defp process_file_size(:error, acc), do: acc

  defp process_file_size({file_size, _}, %DaySeven{current_dir: current_dir} = acc) do
    updated_dirs_with_current_file =
      acc.dirs
      |&gt; Map.update!(current_dir, fn existing_value -&gt; existing_value + file_size end)

    updated_dirs =
      acc.parent_dirs
      |&gt; Enum.reduce(updated_dirs_with_current_file, fn parent, d_acc -&gt;
        d_acc
        |&gt; Map.update!(parent, fn existing_value -&gt; existing_value + file_size end)
      end)

    %{acc | dirs: updated_dirs}
  end

  defp is_size_less_than_100k?(file_size), do: file_size &lt;= 100_000
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="271685" 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-7/52324/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-271685" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="271685"
                     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="271925" data-post-id="271925">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You should consider the possibility that a directory has subdirectories with the same name.</p> 
	            </div>

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

    </div>

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