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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I solved Part I with a <code>DynamicSupervisor</code> and a process per a fish approach <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>
<p>I expected fish behavioral features are to be changed in Part II and boom.</p> 
	            </div>

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


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

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

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It’s nice when it works on part 2 without modifications. Not the most clever solution but it’s fast enough!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Day06 do
  def part1(input) do
    input
    |&gt; parse_input()
    |&gt; Enum.frequencies()
    |&gt; pass_days(80)
    |&gt; Map.values()
    |&gt; Enum.sum()
  end

  def part2(input) do
    input
    |&gt; parse_input()
    |&gt; Enum.frequencies()
    |&gt; pass_days(256)
    |&gt; Map.values()
    |&gt; Enum.sum()
  end

  def pass_days(fish, 0), do: fish

  def pass_days(fish, days_left) do
    fish
    |&gt; Enum.reduce(%{}, fn
      {0, qt}, acc -&gt;
        acc |&gt; Map.update(6, qt, &amp;(&amp;1 + qt)) |&gt; Map.put(8, qt)

      {n, qt}, acc -&gt;
        acc |&gt; Map.update(n - 1, qt, &amp;(&amp;1 + qt))
    end)
    |&gt; pass_days(days_left - 1)
  end

  def parse_input(input) do
    input |&gt; String.trim() |&gt; String.split(",") |&gt; Enum.map(&amp;String.to_integer/1)
  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="234270" 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-6/44274/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-234270" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234270"
                     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="234273" data-post-id="234273">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution:<br>
I used 2 maps to track two groups of fish. <code>fish</code> cycles 7 days, <code>spawn</code> cycles 9 days.<br>
I couldn’t figure a way to track of all fish at the same rate, so I split into two groups, each with a different “gestation” rate.</p>
<p>Runs ~250ms on Apple M1.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2021.D06 do
  @spec part_one([integer()], integer()) :: integer()
  def part_one(seed, days) do
    map_solution(seed, days)
  end

  @spec part_two([integer()], integer()) :: integer()
  def part_two(seed, days) do
    map_solution(seed, days)
  end

  defp map_solution(seed, days) do
    fish = Enum.frequencies(seed)

    Stream.unfold({0, fish, %{}}, fn
      {day, fish, spawn} -&gt;
        fish_cycle = rem(day, 7)
        spawn_cycle = rem(day, 9)

        num_new_fish = Map.get(spawn, spawn_cycle, 0)
        fish = Map.update(fish, fish_cycle, num_new_fish, &amp;(&amp;1 + num_new_fish))

        spawn = Map.put(spawn, spawn_cycle, Map.fetch!(fish, fish_cycle))

        {{fish, spawn}, {day + 1, fish, spawn}}
    end)
    |&gt; Enum.at(days - 1)
    |&gt; (fn {fish, spawn} -&gt;
          Map.merge(fish, spawn, fn _k, v1, v2 -&gt; v1 + v2 end)
        end).()
    |&gt; Map.values()
    |&gt; Enum.sum()
  end
end

input =
  Path.join(["d06_input.txt"])
  |&gt; File.stream!()
  |&gt; Stream.map(&amp;String.trim/1)
  |&gt; Enum.at(0)
  |&gt; String.split(",")
  |&gt; Enum.map(&amp;String.to_integer/1)

IO.inspect(Advent.Y2021.D06.part_one(input, 80))
IO.inspect(Advent.Y2021.D06.part_two(input, 256))
</code></pre>
<p>I originally had a function <code>list_solution/2</code> I used for part one. It took <em>way</em> too long for part two (I killed attempt before completion). But here it is incase there’s any interest</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@spec list_solution([integer()], integer()) :: integer()
defp list_solution(seed, days) do
  Stream.iterate(seed, fn fish -&gt;
    num_spawning_fish = Enum.count(fish, &amp;(&amp;1 == 0))

    fish =
      Enum.map(fish, fn
        0 -&gt; 6
        n -&gt; n - 1
      end)

    fish ++ List.duplicate(8, num_spawning_fish)
  end)
  |&gt; Enum.at(days)
  |&gt; length()
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="234273" 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-6/44274/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-234273" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234273"
                     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="234275" data-post-id="234275">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Looking at others, I see that updating the map key isn’t expensive, and performance is similar - probably the cleaner/nicer way to go!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234275" 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-6/44274/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-234275" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234275"
                     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="234283" data-post-id="234283">
  <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 with Livebook: <a href="https://github.com/egze/aoc_live/blob/main/y2021/day-06.livemd" rel="noopener nofollow ugc">y2021/day-06.livemd</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="234283" 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-6/44274/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-234283" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234283"
                     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="234284" data-post-id="234284">
  <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>My <a href="https://github.com/juanperi/advent_of_code/blob/master/2021/day_6.ex" rel="noopener nofollow ugc">solution</a><br>
Using frequencies as everybody else <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="234284" 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-6/44274/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-234284" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234284"
                     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="234285" data-post-id="234285">
  <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>Eh, one of the more difficult days for me.<br>
It was immediately clear to me that recursion with big lists is not the way to solve this.<br>
But I was not able to realize that <code>Enum.frequencies</code> is the key to this. Quite frustrating, once again. <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"><br>
I had to take a peek here. Once I saw it mentioned, it was so obvious.<br>
<a href="https://github.com/stefanluptak/advent-of-code/blob/main/day_06.livemd" rel="noopener nofollow ugc">day_06.livemd</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="234285" 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-6/44274/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-234285" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234285"
                     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="234313" data-post-id="234313">
  <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>Mine was very similar to some others here</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">fish
|&gt; Enum.reduce(%{}, fn {timer, count}, acc -&gt;
  if timer &gt; 0 do
    tally_fish(acc, timer - 1, count)
  else
    acc
    |&gt; tally_fish(6, count)
    |&gt; tally_fish(8, count)
  end
end)
</code></pre>
<p>Full solution <a href="https://github.com/APB9785/AoC-2021-elixir/blob/master/lib/day_06.ex" rel="noopener nofollow ugc">here</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="234313" 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-6/44274/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-234313" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234313"
                     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="234334" data-post-id="234334">
  <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>Looking at the replies, nothing special with my solution. Frequencies and updates. Just that I used reduce.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Enum.reduce(1..256, start_state, fn _, state -&gt;
  state
  |&gt; Enum.reduce(%{}, fn
    {0, c}, curr_state -&gt;
      curr_state |&gt; Map.update(6, c, &amp;(c + &amp;1)) |&gt; Map.update(8, c, &amp;(c + &amp;1))

    {age, c}, curr_state -&gt;
      curr_state |&gt; Map.update(age - 1, c, &amp;(c + &amp;1))
  end)
end)
|&gt; Map.values()
|&gt; Enum.sum()
</code></pre>
<p>Full Livebook <a href="https://github.com/Klohto/AdventOfCode/blob/main/2021/day6.livemd" rel="noopener nofollow ugc">https://github.com/Klohto/AdventOfCode/blob/main/2021/day6.livemd</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="234334" 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-6/44274/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-234334" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234334"
                     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/44274/load_more?page=3">Load more posts (8 remaining)</a>
</div></template></turbo-stream>