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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>how do you know to do the bitwise NOT and then AND with 0b111111111111? I thought of negation using the NOT operator but it gives this crazy negative number. What’s that about?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234042" 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-2021-day-3/44230/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-234042" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234042"
                     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="234046" data-post-id="234046">
  <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 noticed that for each bit in the binary numbers, if the majority is 1, then the minority is definitely 0, and vice versa. So if the corresponding bit in <code>gamma</code> is 1, then that bit in <code>epsilon</code> is definitely 0.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234046" 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-2021-day-3/44230/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-234046" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234046"
                     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="234049" data-post-id="234049">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>No, I get that. I mean, why doesn’t the <code>~~~</code> operator work without the <code>&amp;&amp;&amp; 0b111111111111</code>?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234049" 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-2021-day-3/44230/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-234049" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234049"
                     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="234050" data-post-id="234050">
  <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 added <code>&amp;&amp;&amp; 0b111111111111</code> just for safety. I don’t know how many bits does an integer contain by default, but probably more than 12 bits.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234050" 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-2021-day-3/44230/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-234050" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234050"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bjorng" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bjorng/120/13187_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bjorng
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Erlang Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="stevensonmt" data-post="22" data-topic="44230" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stevensonmt/48/20503_2.png" class="avatar"> stevensonmt:</div>
<blockquote>
<p>how do you know to do the bitwise NOT and then AND with 0b111111111111? I thought of negation using the NOT operator but it gives this crazy negative number. What’s that about?</p>
</blockquote>
</aside>
<p>The <code>~~~</code> operator when given a positive number always returns a negative number. It does that simulate that an integer holds an infinite number of bits. If the number starts with an infinite number of ones, the number is negative. If it starts with an infinite number of zeroes, it is positive.</p>
<p>When I first learned Erlang, it took me a while to figure out why that’s make sense.</p>
<aside class="quote no-group" data-username="Aetherus" data-post="25" data-topic="44230">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/48/17203_2.png" class="avatar"> Aetherus:</div>
<blockquote>
<p>I don’t know how many bits does an integer contain by default, but probably more than 12 bits.</p>
</blockquote>
</aside>
<p>Yes, an infinite number of bits. Fortunately the runtime system is smart enough to not store all them explicitly. <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234059" data-batch-url="/posts/batch_likers">
                        5
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-3/44230/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-234059" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234059"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution: <a href="https://github.com/josevalim/aoc/blob/main/2021/day-03.livemd" rel="nofollow">https://github.com/josevalim/aoc/blob/main/2021/day-03.livemd</a></p>
<p>Live streaming: <a href="https://www.twitch.tv/videos/1223929784" class="inline-onebox" rel="nofollow">Twitch</a> - we also solved part 1 with Nx and had a bit of fun with Livebook. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" 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="234116" data-batch-url="/posts/batch_likers">
                        4
                      </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-2021-day-3/44230/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-234116" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234116"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Did you put that <code>&lt;!-- vim: syntax=markdown --&gt;</code> on top of your <code>livemd</code> file manualy or is that some setting in the Livebook?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234126" 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-2021-day-3/44230/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-234126" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234126"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="josevalim" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/120/1787_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  josevalim
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Manually. Livebook will preserve it though (but maybe it requires latest).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234128" 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-2021-day-3/44230/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-234128" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234128"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>so negation in erlang flips all the infinite preceding zeroes? Is there a practical reason for that? Is it just to avoid the need for distinct sized integer types like u8, u16, i32, etc? Would you always get an accurate answer by masking with ones in all places up to the bitsize you need? Would it be safe to just mask by 4294967295 unless your values exceed u32 max size?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234136" 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-2021-day-3/44230/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-234136" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234136"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sure we are at Day 4 now and nobody will read it, but still my solution for the Day 3, part one + tests.<br>
Kind of ugly solution, but I was trying to use pattern matching as much as I can and was trying to avoid iteration all the way.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC_2021.Day3 do
  @input "input_3.txt"

  @initial_list_of_tuples [{"0", 0}, {"1", 0}]
  @acc [
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples,
    @initial_list_of_tuples
  ]

  def part_one do
    @input
    |&gt; read_file()
    |&gt; find_max_min_occ()
    |&gt; to_gamma_epsilon()
    |&gt; count()
  end

  defp find_max_min_occ(list_of_strings) do
    Enum.reduce(
      list_of_strings,
      @acc,
      fn &lt;&lt;first::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;second::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;third::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;fourth::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;fifth::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;six::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;seven::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;eight::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;nine::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;ten::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;eleven::binary-size(1)&gt;&gt; &lt;&gt;
           &lt;&lt;twelve::binary-size(1)&gt;&gt;,
         [
           o,
           t,
           th,
           fo,
           fi,
           si,
           se,
           ei,
           ni,
           te,
           el,
           tw
         ] -&gt;
        [
          form_in_order(o, first),
          form_in_order(t, second),
          form_in_order(th, third),
          form_in_order(fo, fourth),
          form_in_order(fi, fifth),
          form_in_order(si, six),
          form_in_order(se, seven),
          form_in_order(ei, eight),
          form_in_order(ni, nine),
          form_in_order(te, ten),
          form_in_order(el, eleven),
          form_in_order(tw, twelve)
        ]
      end
    )
  end

  defp form_in_order([{k1, v1}, {k2, v2}], item) when k1 == item do
    t = [{k1, v1 + 1}, {k2, v2}]
    [{k1, v1}, {k2, v2}] = t
    if v1 &gt; v2, do: [{k1, v1}, {k2, v2}], else: [{k2, v2}, {k1, v1}]
  end

  defp form_in_order([{k1, v1}, {k2, v2}], _item), do: [{k1, v1}, {k2, v2 + 1}]

  defp to_gamma_epsilon(list_of_maps) do
    Enum.reduce(list_of_maps, {"", ""}, fn [{k1, _}, {k2, _}], {gm, ep} -&gt;
      {gm &lt;&gt; k1, ep &lt;&gt; k2}
    end)
  end

  defp count({gm, ep}) do
    gm = Integer.parse(gm, 2) |&gt; elem(0)
    ep = Integer.parse(ep, 2) |&gt; elem(0)
    gm * ep
  end

  defp read_file(input) do
    input
    |&gt; File.read!()
    |&gt; String.trim()
    |&gt; String.split(["\n"])
  end
end

case System.argv() do
  ["--test"] -&gt;
    ExUnit.start()

    defmodule AOC_2021.Day3Test do
      use ExUnit.Case

      @expected_part_one 3_901_196

      test "tests" do
        response = fn _ -&gt;
          [
            AOC_2021.Day3.part_one()
          ]
        end

        assert [@expected_part_one] ==
                 response.(3_901_196)
      end
    end

  ["--run"] -&gt;
    AOC_2021.Day3.part_one()
    |&gt; IO.inspect(label: "\npart_one \n")

  _ -&gt;
    IO.puts(:stderr, "\nplease specify --run or --test flag")
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="234141" 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-2021-day-3/44230/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-234141" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234141"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #30"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/44230/load_more?page=4">Load more posts (5 remaining)</a>
</div></template></turbo-stream>