<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="233972" data-post-id="233972">
  <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>Thanks a lot. I too missed the fact, that I have to recompute the frequency map for each iteration. <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="233972" 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-2021-day-3/44230/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-233972" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233972"
                     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="233975" data-post-id="233975">
  <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>My solution: <a href="https://github.com/egze/aoc/blob/master/lib/aoc/y2021/d3.ex" rel="noopener nofollow ugc">y2021/d3.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="233975" 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/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-233975" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233975"
                     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="233978" data-post-id="233978">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is my solution: <a href="https://git.xirion.net/0x76/aoc2021/src/branch/main/lib/day03.ex" rel="noopener nofollow ugc">https://git.xirion.net/0x76/aoc2021/src/branch/main/lib/day03.ex</a><br>
I tried my hand at using Nx for part 1 but I didn’t know to effectively use it for the filter part of part 2</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s mine: <a href="https://github.com/wasi0013/advent_of_code/blob/master/lib/y2021/day_03.ex" rel="noopener nofollow ugc">y2021/day_03.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="233979" 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/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-233979" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233979"
                     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="233993" data-post-id="233993">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a href="https://github.com/APB9785/AoC-2021-elixir/blob/master/lib/day_03.ex" rel="noopener nofollow ugc">My solution</a> basically revolves around</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp count_ones_per_index(tuple, counts) do
  Enum.reduce(0..11, counts, fn idx, acc -&gt;
    Map.update!(acc, idx, &amp;(&amp;1 + elem(tuple, idx)))
  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="233993" 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/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-233993" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233993"
                     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="233998" data-post-id="233998">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Livebook again <img src="https://forum.elixirforum.com/images/emoji/apple/wave.png?v=15" title=":wave:" class="emoji" alt=":wave:" loading="lazy" width="20" height="20"> I don’t know why I keep working just with anonymous functions but it was kinda fun (pun intended).</p>
<p>Turns out that anonymous functions don’t support recursion but I found <a href="https://stackoverflow.com/a/21983413" rel="noopener nofollow ugc">José mentioning</a> Elixir will support it in the future (2014). Does anyone know what happened? I hacked around it using by passing fun.</p>
<p>Somehow my Part 2 ended much shorter than Part 1. Mostly thanks to pattern matching and the recursion logic.</p>
<p>As always, just copy the code and you can import it directly into the Livebook <img src="https://forum.elixirforum.com/images/emoji/apple/pray.png?v=15" title=":pray:" class="emoji" alt=":pray:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Day 2

## Input

&lt;!-- livebook:{"livebook_object":"cell_input","name":"input","type":"text","value":"00100 11110 10110 10111 10101 01111 00111 11100 10000 11001 00010 01010"} --&gt;

```elixir
processed =
  IO.gets(:input)
  |&gt; String.split(~r{\s}, trim: true)
```

## Part 1

```elixir
# helpers
ind_len = String.length(Enum.at(processed, 0))
convert = fn data_list -&gt; Enum.join(data_list) |&gt; String.to_integer(2) end
###

gamma =
  processed
  |&gt; Enum.reduce(
    List.duplicate(0, ind_len),
    fn bits, stats -&gt;
      bits
      |&gt; String.graphemes()
      |&gt; Enum.with_index()
      |&gt; Enum.map(fn {c, index} -&gt;
        Enum.at(stats, index) + String.to_integer(c)
      end)
    end
  )
  |&gt; Enum.map(fn n_ones -&gt;
    if n_ones &gt; div(length(processed), 2), do: 1, else: 0
  end)
  |&gt; convert.()

epsilon =
  gamma
  |&gt; Bitwise.~~~()
  |&gt; Bitwise.&amp;&amp;&amp;(String.duplicate("1", ind_len) |&gt; String.to_integer(2))

gamma * epsilon
```

## Part 2

```elixir
# helpers
convert = fn data -&gt; Enum.join(data) |&gt; String.to_integer(2) end
###

converted_entries =
  processed
  |&gt; Enum.map(fn bits -&gt;
    String.graphemes(bits) |&gt; Enum.map(&amp;String.to_integer/1)
  end)

# Expects:
# list -&gt; list of split converted bits eq.: [[0,1,0],[1,0,1]]
# index -&gt; starting index eq.: 0
# sorter -&gt; which value takes precendence when comparing eq.: &amp;&gt;/2
b_reduce = fn list, index, sorter -&gt;
  Enum.group_by(list, &amp;Enum.at(&amp;1, index))
  |&gt; Enum.max_by(fn {_k, v} -&gt; length(v) end, sorter)
  |&gt; elem(1)
end

slvr = fn
  [final], _, _, _ -&gt; convert.(final)
  list, i, sorter, fun -&gt; b_reduce.(list, i, sorter) |&gt; fun.(i + 1, sorter, fun)
end

slvr.(converted_entries, 0, &amp;&gt;/2, slvr) * slvr.(converted_entries, 0, &amp;&lt;=/2, slvr)
```

</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="233998" 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/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-233998" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233998"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Klohto" data-post="17" 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/klohto/48/26029_2.png" class="avatar"> Klohto:</div>
<blockquote>
<p>Does anyone know what happened?</p>
</blockquote>
</aside>
<p>AFAIK Core lost interest in it as the use case for that was mostly the shell and in IEx you can generate modules on the fly, which is not easily doable in Erlang Shell. However if you want to achieve the same thing, then you can use power of macros for that:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Foo do
  defmacro fn_rec(do: body) do
    {name, argc, body} =
      body
      |&gt; Enum.map(fn
        {:'-&gt;', e1, [[{:when, e2, [{name, _, args}, guard]}], body]} -&gt;
          {name, length(args), {:'-&gt;', e1, [[{:when, e2, args ++ [guard]}], body]}}

        {:'-&gt;', e1, [[{name, _, args}], body]} -&gt;
          {name, length(args), {:'-&gt;', e1, [args, body]}}
      end)
      |&gt; Enum.reduce(fn {name, argc, body}, {name, argc, acc} -&gt;
        {name, argc, List.wrap(acc) ++ [body]}
      end)

    args = Macro.generate_arguments(argc, __CALLER__.module)
    func = {:fn, [], body}
    name = {name, [], Elixir}

    quote do
      tmp =
        fn f -&gt;
          var!(unquote(name)) = fn unquote_splicing(args) -&gt; f.(f).(unquote_splicing(args)) end
          unquote(func)
        end

      tmp.(tmp)
    end
  end

  def run do
    ack =
      fn_rec do
        a(0, n) -&gt; n + 1
        a(m, 0) -&gt; a.(m - 1, 1)
        a(m, n) -&gt; a.(m - 1, a.(m, n - 1))
      end

    ack.(3, 4)
  end
end

IO.puts Foo.run
</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="234028" 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-2021-day-3/44230/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-234028" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234028"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And <a href="https://github.com/juanperi/advent_of_code/blob/master/2021/day_3.ex" rel="noopener nofollow ugc">mine</a>.<br>
Part 2 revolves around</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def group_by_position(matrix, position) do
    Enum.group_by(
      matrix,
      fn row -&gt; Enum.at(row, position) end
    )
  end

  def find_most_common([row], _, _) do
    row
  end

  def find_most_common(matrix, position, sorter) do
    grouped = group_by_position(matrix, position)
    {_, max_matrix} = Enum.max_by(grouped, fn {k, v} -&gt; {length(v), k} end, sorter)
    find_most_common(max_matrix, position + 1, sorter)
  end
</code></pre>
<p>and then called as</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">oxigen_generator_rating =
  Support.input()
  |&gt; Support.find_most_common(:gte)
  |&gt; Support.row_to_number()

co2_scrubber_rating =
  Support.input()
  |&gt; Support.find_most_common(:lte)
  |&gt; Support.row_to_number()
</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="234029" 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-2021-day-3/44230/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-234029" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234029"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Late to the party…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day3 do
  def part1 do
    frequencies = load_data()
    |&gt; reshape()
    |&gt; to_frequencies()

    gamma_rate = get_rate(frequencies, &amp;most_common_reducer/2)
    epsilon_rate = get_rate(frequencies, &amp;least_common_reducer/2)
    gamma_rate * epsilon_rate
  end

  def part2 do
    oxygen_generator_rating = filter_and_pop(load_data(), &lt;&lt;&gt;&gt;, :most)
    co2_scrubber_rating = filter_and_pop(load_data(), &lt;&lt;&gt;&gt;, :least)

    oxygen_generator_rating * co2_scrubber_rating
  end

  defp filter_and_pop([el], result, _mode) do
    String.to_integer(result &lt;&gt; el, 2)
  end

  defp filter_and_pop(list, result, mode) do
    freq = first_position_frequencies(list)
    {zero, one} = {freq["0"], freq["1"]}

    char = case  mode do
      :most -&gt; if zero &gt; one, do: "0", else: "1"
      :least -&gt; if zero &lt;= one, do: "0", else: "1"
    end

    popped = list
    |&gt; Enum.filter(&amp;String.starts_with?(&amp;1, char))
    |&gt; Enum.map(fn &lt;&lt;_::utf8, rest::binary&gt;&gt; -&gt; rest end)

    filter_and_pop(popped, result &lt;&gt; char, mode)
  end

  defp first_position_frequencies(list) do
    list
    |&gt; Enum.map(&amp;String.first(&amp;1))
    |&gt; Enum.frequencies()
  end

  def load_data do
    "input.txt"
    |&gt; File.read!()
    |&gt; String.split("\n", trim: true)
  end

  defp reshape([h|_] = list) do
    initial = List.duplicate(&lt;&lt;&gt;&gt;, String.length(h))

    Enum.reduce(list, initial, fn x, acc -&gt;
      record = String.split(x, "", trim: true)

      acc
      |&gt; Enum.zip(record)
      |&gt; Enum.map(fn {full, add} -&gt;
        full &lt;&gt; add
      end)
    end)
  end

  defp to_frequencies(list) when is_list(list),
    do: Enum.map(list, &amp;to_frequency(&amp;1))

  defp to_frequency(string) when is_binary(string) do
    string
    |&gt; String.split("", trim: true)
    |&gt; Enum.frequencies()
  end

  def get_rate(frequencies, reducer) do
    frequencies
    |&gt; Enum.reduce(&lt;&lt;&gt;&gt;, reducer)
    |&gt; String.to_integer(2)
  end

  defp most_common_reducer(%{"0"=&gt;zero, "1"=&gt;one}, acc),
    do: if zero &gt; one, do: acc &lt;&gt; "0", else: acc &lt;&gt; "1"

  defp least_common_reducer(%{"0"=&gt;zero, "1"=&gt;one}, acc),
    do: if zero &lt; one, do: acc &lt;&gt; "0", else: acc &lt;&gt; "1"
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="234038" 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/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-234038" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234038"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wanted to keep it pipeline-y. I feel like there has to be a way to do something similar in one pass, but I couldn’t figure it out before Day 4.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Part 1
"day3.txt"
|&gt; File.stream!
|&gt; Enum.map(fn line -&gt; String.split(line, "", trim: true) end)
|&gt; Enum.zip_with(&amp;Enum.frequencies/1)
|&gt; Enum.map(fn counts -&gt; if counts["0"] &gt; counts["1"], do: ["0", "1"], else: ["1", "0"] end)
|&gt; Enum.zip_with(&amp;Enum.join/1)
|&gt; Enum.reduce(1, fn number, product -&gt; String.to_integer(number, 2) * product end)

# Part 2
defmodule Day3 do
  def input() do
    "day3.txt"
    |&gt; File.stream!
    |&gt; Enum.map(&amp;String.trim/1)
  end

  def oxygen([final_value], _position) do
    String.to_integer(final_value, 2)
  end

  def oxygen(data, position) do
    data
    |&gt; Enum.group_by(fn num -&gt; String.at(num, position) end)
    |&gt; Enum.max_by(&amp;number_sorter/1)
    |&gt; elem(1)
    |&gt; oxygen(position + 1)
  end

  def carbon([final_value], _position) do
    String.to_integer(final_value, 2)
  end

  def carbon(data, position) do
    data
    |&gt; Enum.group_by(fn num -&gt; String.at(num, position) end)
    |&gt; Enum.min_by(&amp;number_sorter/1)
    |&gt; elem(1)
    |&gt; carbon(position + 1)
  end

  defp number_sorter({target_bit, bit_list}) do
    length(bit_list) + 0.1 * String.to_integer(target_bit)
  end
end

data = Day3.input()
Day3.oxygen(data, 0) * Day3.carbon(data, 0)
</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="234041" 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/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-234041" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234041"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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