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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/bjorng" rel="nofollow">@bjorng</a> did something similar.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a href="https://topaz.github.io/paste/#XQAAAQBdBgAAAAAAAAAyGUkFUehZxfOWmzTWjQS4wLVJDMV53NKyIELeBc5Z3yL9PIHPcb7pCuU37yVLrPIYOfo+OwRqyqChKjGqk3YF6KAvT2nQG+s1+3nxWRKkH2TXzVuE0CtozwDbVvYTb7V5u9mwjMLWKSFiH+3BNjqtraSMHroc8D1s4EpnC+O50IyF6m5TPrP1Cm3x4sfeSxda5KycjgUS1c93HiR9GjkqhXOgFMG11V9SDiB2o3bLNdA4N9O92EZ4KirL6+4bK/CaApcMX7wtSVIJyZ2gmtgqG+XqCnGWZMkTG2AYkhegjgM2Qc4Wxl5s4hp4j3ruSAE28iiPcXzu1awLRbHtXp+N5GgsE7igzSnbCiGZzEYyZky/NVERvIbRvpdFXJ3JksIcYBAzACC4p8KxoDeSXKKecqeY0o9pgM/d/1AVQ3thvsvhOgJ58ZUGGHRGraZ0IMyjed5dyh59p/GhR5l9wk1lLdXPApW+J2wnIDZAvdKIsiUk6cb7fjvzQQilq8ciZIEVg1ee6Em4QVMGZAboRegErKA1x8jYHIPWtTs3OHhgVor1LIhu4W4Z81I2f2Bi/Xr0cjvJamXzO7x+jK/8PrWT1LEGFzY5FwHqRPLA9sy04htvak+F/foD/cgczQf4r3qPuQRVhf29oTf7SzGrzsir8j/Uq7RysHBBhMfm/qd603nwy7AaHoil/X7A956YFeS7rTsL6naYcTxZoc6O7pTZx83vx4jF27NAmPjw+yIpDqhA7zO/7X7jDo0D7Y1GeoGpLSJHfVjY2SFW1+YkAFfEL+ki/U8n/w==" rel="noopener nofollow ugc">paste</a></p>
<p>Didn’t work out too slow. Part 2 took about 20s on my laptop.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Whatever works as long as you get the answer <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="350634" 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-2024-day-22/68288/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-350634" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350634"
                     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="350656" data-post-id="350656">
  <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">
								<p>Today was easy:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Buyer do
  def hash(number, rounds \\ 2000) do
    Enum.map_reduce(1..rounds//1, number, fn _, acc -&gt;
      r = do_round(acc)
      {rem(r, 10), r}
    end)
  end

  defp do_round(num) do
    num = subround(num, num * 64)
    num = subround(num, div(num, 32))
    subround(num, num * 2048)
  end

  defp subround(a, b), do: rem(Bitwise.bxor(a, b), 16777216)

  def sequences(prices) do
    prices
    |&gt; Enum.chunk_every(5, 1, :discard)
    |&gt; Enum.reduce(%{}, fn seq, map -&gt;
      Map.put_new(map, diffs(seq), List.last(seq))
    end)
  end

  def diffs(vals), do: vals |&gt; Enum.chunk_every(2, 1, :discard) |&gt; Enum.map(fn [a, b] -&gt; b - a end)
end

# P1
inits
|&gt; Enum.map(&amp;elem(Buyer.hash(&amp;1), 1))
|&gt; Enum.sum()

# P2
seqs =
  inits
  |&gt; Task.async_stream(
    fn init -&gt;
      {prices, _} = Buyer.hash(init)

      Buyer.sequences(prices)
    end,
    ordered: false
  )
  |&gt; Enum.reduce(%{}, fn {:ok, a}, b -&gt; Map.merge(a, b, fn _k, v, k -&gt; v + k end) end)

Enum.max_by(seqs, &amp;elem(&amp;1, 1))
</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="350656" 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-2024-day-22/68288/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-350656" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350656"
                     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="350670" data-post-id="350670">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Any idea what’s the fastest value type as a key in a map? Atom maybe?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="350670" 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-2024-day-22/68288/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-350670" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350670"
                     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="350672" data-post-id="350672">
  <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">
								<p>I would expect atoms and small integers (fitting in 60 bits) to be fast when used as map keys.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just FYI. I tried for keys that maps sequences to bananas, unique Integers, Atoms, Tuples, and Strings as keys. Here are the times for the final bunch of merges that produce the answer.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Integer 747ms
# Atom 670ms
# Tuple 1024ms
# String 2371ms
</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="350679" 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-2024-day-22/68288/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-350679" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350679"
                     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="350695" data-post-id="350695">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>I wonder what optimizations could be made other than memoizing.</p>
</blockquote>
<p>I ran brute force to get the solution, then optimized that.  If you know when to stop adding bananas for a specific pricing sequence, you can skip a lot of computation.</p>
<p>Compute the min and max unit price for the pricing sequence; the most bananas you can get is max unit price * monkeys with that pricing sequence.  You can track the maximum while computing a banana run to short circuit early.</p>
<p>Then if you order the pricings based on their expected average price, you can start short circuiting earlier using the more likely “best banana” price sequence results.<br>
UPDATE: optimized even further - I no longer needed this strategy. Instead, I just loop through each pricing on each monkey, check to make sure it’s the first pricing seen, and add it to the total bananas in a pricing hash table of total bananas.  Max on that table is the result.<br>
Running now in about 400ms.</p>
<p>–<br>
I just compute the total bananas and keep track of a pricing<br>
In my code, it cut the number of pricing sequences additions I had to compute from brute force ~4.5M to ~118K.</p>
<p>For price sequence D1 - D4, the magic calculations were these:</p>
<pre><code>    int MinimumUnitPrice =&gt;
        Max(0, Max(0, Max(0, Max(0, 0 - D1) - D2) - D3) - D4)
            + D4 + D3 + D2 + D1;

    int MaximumUnitPrice =&gt;
        Min(9, Min(9, Min(9, Min(9, 9 - D1) - D2) - D3) - D4)
            + D4 + D3 + D2 + D1;
</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="350695" 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-2024-day-22/68288/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-350695" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350695"
                     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="350696" data-post-id="350696">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="trenttobler" data-post="19" data-topic="68288">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/trenttobler/48/37032_2.png" class="avatar"> trenttobler:</div>
<blockquote>
<p>Running now in about 400ms</p>
</blockquote>
</aside>
<p>In C#? This is the Elixir forum so sharing the Elixir running time would also be helpful. Elixir being immutable can be quite inefficient when doing things that involve many updates to data structures.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="350696" 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-2024-day-22/68288/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-350696" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350696"
                     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="350709" data-post-id="350709">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeah, it’s C#. I’m not as fluent with elixir, but here’s the immutable implementation of my latest strategy (using mostly linq query syntax – hopefully, the logic comes through):</p>
<pre><code>    var bananasByPricing =
        from initialMonkeySecret in initialBuyerSecrets
        let monkeySecrets = EvolveRep2001(initialMonkeySecret)
        let bananaPricePoints =
            from pricing in Pricings(monkeySecrets).DistinctBy(pricing =&gt; pricing.Key)
            select (pricing.Key, bananas: pricing.Bananas)
        from bananaPricePoint in bananaPricePoints
        group bananaPricePoint.bananas by bananaPricePoint.Key
        into pricePointBananas
        select (totalBananas: pricePointBananas.Sum(), pricing: pricePointBananas.Key);

    var answer = bananasByPricing.MaxBy(p =&gt; p.totalBananas);
</code></pre>
<p>Notes: “Pricing” key is a 4-tuple on price differences (D1-D4 values), and the “bananas” result for that price point,  EvolveRep2001 generates a sequence of 2001 secrets from the initial secret.  DistinctBy and MaxBy will choose the first qualifying element in the sequence.</p>
<p>Apologies if you wanted elixir directly - I posted more on the comment/question about possible further optimizations that might exist.  My original idea was noting that the sum could potentially short circuit using an aggregation/reduce step, that could stop early rather than continuing to add more bananas once it was determined that there would not be enough remaining pricing results to exceed a current running maximum.  You’re correct to point out that it could be a very different performance characteristic on elixir, and while I agree that there are some algorithms that may suffer due to pure functional immutability, I do not believe this is one of them.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="350709" 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-2024-day-22/68288/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-350709" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="350709"
                     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/68288/load_more?page=3">Load more posts (2 remaining)</a>
</div></template></turbo-stream>