<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="270489" data-post-id="270489">
  <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>Yea, MapSet all the way on this one <img src="https://forum.elixirforum.com/images/emoji/apple/metal.png?v=15" title=":metal:" class="emoji" alt=":metal:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ExAOC2022.Day4 do
  @input "./lib/day4_input.txt"

  def puzzle1() do
    @input
    |&gt; file_by_line()
    |&gt; Enum.map(&amp;line_to_elfarea/1)
    |&gt; Enum.map(&amp;subset?/1)
    |&gt; Enum.reject(&amp; !&amp;1)
    |&gt; length()
  end

  def puzzle2() do
    @input
    |&gt; file_by_line()
    |&gt; Enum.map(&amp;line_to_elfarea/1)
    |&gt; Enum.map(&amp;intersect?/1)
    |&gt; Enum.reject(&amp; !&amp;1)
    |&gt; length()
  end

  defp line_to_elfarea(line) do
    line
    |&gt; String.split(",")
    |&gt; IO.inspect()
    |&gt; Enum.map(&amp;to_map_set/1)
  end

  defp to_map_set(area_str) do
    [first, last] = String.split(area_str, "-")

    Range.new(String.to_integer(first), String.to_integer(last))
    |&gt; Enum.to_list
    |&gt; MapSet.new()
  end

  defp subset?([one, two]) do
    MapSet.subset?(one, two) || MapSet.subset?(two, one)
  end

  defp intersect?([one, two]) do
    MapSet.size(MapSet.intersection(one, two)) &gt; 0
  end

  defp file_by_line(file) do
    file
    |&gt; File.read!()
    |&gt; String.split(~r/\R/, trim: true)
  end
end
</code></pre> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="270603" data-post-id="270603">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>simple Elixir, using <code>MapSet.subset?</code> and <code>MapSet.disjoint?</code>, both questions at the same time</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">File.stream!("input")
|&gt; Enum.reduce({0,0}, fn line, {acc_part1, acc_part2} -&gt;
    [s1, e1, s2, e2] = line
      |&gt; String.split(~r/\D/, trim: true)
      |&gt; Enum.map(&amp;String.to_integer/1)
    {m1, m2} = {MapSet.new(s1..e1), MapSet.new(s2..e2)}
    {
      acc_part1 + (if MapSet.subset?(m1,m2) || MapSet.subset?(m2,m1), do: 1, else: 0),
      acc_part2 + (if MapSet.disjoint?(m1,m2), do: 0, else: 1),
    }
end)
|&gt; IO.inspect()
</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="270603" 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-4/52239/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-270603" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270603"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="270801" data-post-id="270801">
  <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>I went with a recursive solution for practice</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day4 do
  def getInput() do
    File.read!("inputs/day-4-input.txt")
    |&gt; String.split("\n")
    |&gt; Enum.map(fn line -&gt;
      line
      |&gt; String.split(",")
      |&gt; Enum.map(fn orders -&gt;
        orders
        |&gt; String.split("-")
        |&gt; Enum.map(&amp;String.to_integer/1)
      end)
    end)
  end

  def score(x), do: if x, do: 1, else: 0

  def contains?([[a, b], [x, y]]) do
    (a &gt;= x and b &lt;= y) or (a &lt;= x and b &gt;= y)
  end

  def containing([]), do: 0
  def containing([pair | rest]) do
    score(contains? pair) + containing rest
  end

  def solution1() do
    getInput() |&gt; containing
  end

  def overlaps?([[a, b], [x, y]]) do
    (b &gt;= x and a &lt;= y) or (y &gt;= a and x &lt;= b)
  end

  def overlapping([]), do: 0
  def overlapping([pair | rest]) do
    score(overlaps? pair) + overlapping rest
  end

  def solution2() do
    getInput() |&gt; overlapping
  end
end

IO.puts Advent.Day4.solution1

IO.puts Advent.Day4.solution2

</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="270801" 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-4/52239/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-270801" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270801"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="270881" data-post-id="270881">
  <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>I decided to look into <code>Regex</code>, which I have never used before in Elixir. Here’s what I came up with, I don’t really like the part where I’m converting the strings to integers since this involves going through nested lists. I would have liked to do this somehow within <code>Regex.scan/3</code>, but I don’t know how. Maybe someone knows if it’s possible?</p>
<p>I’m happy with everything else. Especially since doing part 2 simply meant turning the <code>and</code> into <code>or</code> for the guard clauses in the <code>Enum.reduce/3</code>.</p>
<p><a href="https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day4.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day4.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="270881" 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-4/52239/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-270881" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270881"
                     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>