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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was happy to see a problem today that didn’t take me hours to solve.<br>
While I knew there was an equation to solve it in an instant, I didn’t bother to look it up and just iterated over the whole problem space.<br>
I fully expected an order of magnitude that required the mathematical approach in part 2, but was surprised that my trivial approach just took 4 seconds to run on part 2.<br>
(Using Erlang for the fun right now:)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">a(In) -&gt;
    [T, D] = [parse(L) || L &lt;- In],
    G = [opt(Time, Dist) || {Time, Dist} &lt;- lists:zip(T, D)],
    lists:foldl(fun(X, Acc) -&gt; X * Acc end, 1, G).

parse(L) -&gt; [list_to_integer(I) || I &lt;- tl(re:split(L, "\\s+", [trim, {return, list}]))].

opt(Time, Dist) -&gt; length([1 || R &lt;- lists:seq(1, Time), R * (Time - R) &gt; Dist]).

b(In) -&gt;
    [T, D] = [list_to_integer(parse_join(L)) || L &lt;- In],
    opt(T, D).

parse_join(L) -&gt; lists:concat([I || I &lt;- tl(re:split(L, "\\s+", [trim, {return, list}]))]).
</code></pre>
<p>There might even be an off by 1 error in my code (<code>&gt; Dist</code> or  <code>&gt;= Dist</code>?), but it still worked on both example an my input.<br>
Probably I’ll clean it up and try to implement the efficient solution in the evening, thanks for the great explanation <a class="mention" href="/u/aetherus" rel="nofollow">@Aetherus</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="310406" 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-2023-day-6/60196/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-310406" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310406"
                     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="310429" data-post-id="310429">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I did write out the quadratic equation for this one! But I couldn’t remember how to solve it, so I just brute forced the solution. It took 70 seconds.</p>
<p><a href="https://github.com/midouest/advent-of-code-2023/blob/main/notebooks/day06.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/midouest/advent-of-code-2023/blob/main/notebooks/day06.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="310429" 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-2023-day-6/60196/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-310429" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310429"
                     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="310440" data-post-id="310440">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It is always fun to see code changes between parts. But this time only input file had to be changed <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 used quadratic equation and just in case assumed error of up to 1.0.</p>
<p><a href="https://gitlab.com/mrsk/aoc-elixir/-/blob/main/lib/Aoc2023/D06.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://gitlab.com/mrsk/aoc-elixir/-/blob/main/lib/Aoc2023/D06.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="310440" 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-2023-day-6/60196/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-310440" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310440"
                     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="310483" data-post-id="310483">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was wondering why this is so slow in Elixir compared to Erlang. I thought your solution should be faster because you used Streams so it doesn’t have to initialize the List upfront. However, in Elixir Livebook it took &gt;100s to complete, while my primitive Erlang solution was done in &lt;4s.</p>
<p>Then I tried it in the Eshell and it was as slow as Elixir.<br>
Livebook: 73s<br>
IEx: 50s<br>
<code>elixir bench.ex</code>: 3.2s<br>
for the exact same code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">for r &lt;- 1..46828479, r * (46828479 - r) &gt; 347152214061471 do 1 end |&gt; Enum.count() |&gt; IO.puts
</code></pre>
<p>Does anyone have an idea why Livebook and IEx is so much slower? Is it possible it’s not doing any JIT, or am I doing something wrong?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]
Elixir 1.15.7 (compiled with Erlang/OTP 25)
Livebook 0.11.4
</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="310483" 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-2023-day-6/60196/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-310483" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310483"
                     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="310486" data-post-id="310486">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="j4nk" data-post="15" data-topic="60196">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/j4nk/48/36692_2.png" class="avatar"> j4nk:</div>
<blockquote>
<p>Does anyone have an idea why Livebook and IEx is so much slower? Is it possible it’s not doing any JIT, or am I doing something wrong?</p>
</blockquote>
</aside>
<p>This is due to compiled versus interpreted code.</p>
<p>In this case, wrapping this within a module inside livebook/IEx will compile the code and run in ~1s, while just running the code directly in interpreted mode (uses <a href="https://www.erlang.org/doc/man/erl_eval.html" rel="nofollow"><code>:erl_eval</code></a> under the hood) will be slow when you have many iterations.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyCell do
  def run do
    for r &lt;- 1..46828479, r * (46828479 - r) &gt; 347152214061471 do 1 end |&gt; Enum.count()
  end
end

MyCell.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="310486" 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-2023-day-6/60196/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-310486" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310486"
                     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="310493" data-post-id="310493">
  <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>I realised we could avoid computing the whole range and get away with only half of it, as the times once you go over time/2 repeat:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def count_winning(race, record) do
    scores = for n &lt;- 1..div(race, 2), do: n * (race - n)
    [_middle | rev_no_middle] = reversed = Enum.reverse(scores)
    scores = scores ++ if rem(race, 2) == 1, do: reversed, else: rev_no_middle
    Enum.count(scores, fn score -&gt; score &gt; record end)
  end
</code></pre>
<p>However I didn’t notice we could use the quadratic equation, or solve it with binary search. Good job I checked the forum otherwise I’d be feeling pretty clever. Thanks for sharing <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>edit: whoops, I even forgot to implement the part that only counts half the list <img src="https://forum.elixirforum.com/images/emoji/apple/man_facepalming.png?v=15" title=":man_facepalming:" class="emoji" alt=":man_facepalming:" loading="lazy" width="20" height="20"></p>
<p><a href="https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2023/day6.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2023/day6.exs</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="310493" 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-2023-day-6/60196/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-310493" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310493"
                     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="310498" data-post-id="310498">
  <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>Simple one today</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp ways_to_win(race) do
  Enum.count(1..race.time, fn held_seconds -&gt;
    traveled = held_seconds * (race.time - held_seconds)
    traveled &gt; race.distance
  end)
end
</code></pre>
<p>Full solution <a href="https://github.com/APB9785/AoC-2023-elixir/blob/master/lib/advent_2023/day_06.ex" rel="noopener nofollow ugc">here on Github</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="310498" 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-2023-day-6/60196/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-310498" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310498"
                     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="310940" data-post-id="310940">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I used the quadratic formula, so part 2 was a non-issue.</p>
<p>The trickiest part of this was a corner-case for some inputs where the racer can end up getting <em>exactly</em> the same distance as the record, which shouldn’t count. I solved this by adding a tiny additional distance, breaking the numerical coincidence.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day6Part1 do
  @regex ~r{Time:\s+([\d\s]+\d)\s+Distance:\s+([\d\s]+\d)}

  def read(filename) do
    data = File.read!(filename)

    [time_str, distance_str] = Regex.run(@regex, data, capture: :all_but_first)

    Enum.zip(parse(time_str), parse(distance_str))
  end

  defp parse(s) do
    s
    |&gt; String.split(~r{\s+})
    |&gt; Enum.map(&amp;String.to_integer/1)
  end

  def endpoints({t, d}) do
    base = t/2
    half_width = :math.sqrt(t*t/4 - (d+0.001))
    {ceil(base - half_width), floor(base + half_width)}
  end
end

Day6Part1.read("input.txt")
|&gt; Enum.map(&amp;Day6Part1.endpoints/1)
|&gt; Enum.map(fn {a, b} -&gt; b - a + 1 end)
|&gt; Enum.product()
|&gt; IO.inspect()
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="310940" 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-2023-day-6/60196/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-310940" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310940"
                     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="312734" data-post-id="312734">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m very late for the party. But here’s my solution.</p>
<p>I didn’t know about the quadratic equation, that’s very cool.</p>
<p>My approach was this: there’s a pattern where after you get the number of holding times that failed to beat the best distance until the first time that beats it, you can use that number to calculate the holding times to beat the distance by multiplying it by 2 and then subtract it by the race_time + 1 (the + 1 is to include the scenario for the 0 holding time)</p>
<p>The main piece is this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def count_holding_time_to_beat_record(race_time, distance_to_beat) do
    0..race_time
    |&gt; Enum.reduce_while(0, fn holding_time, acc -&gt;
      distance_reached = calculate_distance(holding_time, race_time)

      if distance_reached &gt; distance_to_beat do
        number_of_fails_for_race = acc * 2
        {:halt, race_time + 1 - number_of_fails_for_race}
      else
        {:cont, acc + 1}
      end
    end)
  end
</code></pre>
<p><a href="https://github.com/tiagoavila/advent_of_code_2023/blob/main/lib/day_six.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/tiagoavila/advent_of_code_2023/blob/main/lib/day_six.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="312734" 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-2023-day-6/60196/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-312734" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312734"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>