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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I too knew LCM was part of the key but was not able to figure out how to utilize it correctly. It doesn’t make any appreciable difference, but I made use of the hint that the answer would be at least 100_000_000_000_000 to start counting from <code>100000000000000 + rem(100000000000000, first_bus)</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13 do
  @moduledoc false

  @input "1001287
  13,x,x,x,x,x,x,37,x,x,x,x,x,461,x,x,x,x,x,x,x,x,x,x,x,x,x,17,x,x,x,x,19,x,x,x,x,x,x,x,x,x,29,x,739,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,x,x,x,23"

  def process() do
    @input
    |&gt; String.split("\n", parts: 2, trim: true)
    |&gt; Enum.with_index()
    |&gt; Enum.reduce({}, fn {sub, ndx}, acc -&gt;
      case ndx do
        0 -&gt;
          Tuple.append(acc, String.to_integer(sub))

        _ -&gt;
          Tuple.append(
            acc,
            sub
            |&gt; String.trim_leading()
            |&gt; String.split(",")
            |&gt; Enum.filter(&amp;Kernel.!=("x", &amp;1))
            |&gt; Enum.map(&amp;String.to_integer(&amp;1))
          )
      end
    end)
  end

  def shortest_wait(timestamp, bus_list) do
    {bus, wait} =
      bus_list
      |&gt; Enum.map(fn x -&gt; {x, x - rem(timestamp, x)} end)
      |&gt; Enum.min_by(fn {_x, wait} -&gt; wait end)

    bus * wait
  end

  def part_1 do
    {timestamp, bus_list} = process()
    shortest_wait(timestamp, bus_list)
  end

  def process_2 do
    @input
    |&gt; String.split("\n", parts: 2, trim: true)
    |&gt; List.last()
    |&gt; String.trim_leading()
    |&gt; String.split(",")
    |&gt; Enum.with_index()
    |&gt; Enum.reject(fn {c, _ndx} -&gt; c == "x" end)
    |&gt; Enum.map(fn {c, ndx} -&gt; {ndx, String.to_integer(c)} end)
  end

  def timestamp_search([{_ndx, first_bus} | _rest] = buses) do
    min_time = 100_000_000_000_000 + rem(100_000_000_000, first_bus)
    timestamp_search(buses, {min_time, 1})
  end

  def timestamp_search([], {timestamp, _}) do
    timestamp
  end

  def timestamp_search([{ndx, bus} | rest] = buses, {timestamp, step}) do
    if valid_timestamp?(timestamp, bus, ndx) do
      timestamp_search(rest, {timestamp, Day13.MathHelpers.lcm(step, bus)})
    else
      timestamp_search(buses, {timestamp + step, step})
    end
  end

  def valid_timestamp?(timestamp, bus, ndx) do
    rem(timestamp + ndx, bus) == 0
  end

  def part_2 do
    process_2()
    |&gt; timestamp_search()
  end

  defmodule MathHelpers do
    @moduledoc false
    def gcd(a, 0), do: a
    def gcd(0, b), do: b
    def gcd(a, b), do: gcd(b, rem(a, b))

    def lcm(0, 0), do: 0
    def lcm(a, b), do: div(a * b, gcd(a, b))
  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="197351" 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-2020-day-13/36180/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-197351" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197351"
                     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="197352" data-post-id="197352">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It could use some cleanup…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13 do
  def readinput() do
    [start, busdata] =
      File.read!("13.input.txt")
      |&gt; String.split("\n", trim: true)

    [String.to_integer(start), String.split(busdata, ",")]
  end

  def part1([start, buses] \\ readinput()) do
    buses =
      buses
      |&gt; Enum.filter(&amp;(&amp;1 != "x"))
      |&gt; Enum.map(&amp;String.to_integer/1)

    bus =
      Enum.map(buses, fn bus -&gt; {bus, bus - rem(start, bus)} end)
      |&gt; Enum.min(fn {_b1, t1}, {_b2, t2} -&gt; t1 &lt; t2 end)

    elem(bus, 0) * elem(bus, 1)
  end

  def part2([_, buses] \\ readinput()) do
    buses =
      buses
      |&gt; Enum.map(fn b -&gt; if b != "x", do: String.to_integer(b), else: "x" end)
      |&gt; Enum.with_index()
      |&gt; Enum.filter(fn {b, _} -&gt; b != "x" end)

    first =
      buses
      |&gt; List.first()
      |&gt; elem(0)

    loop(buses, first)
  end

  def loop(buses, time) do
    mods =
      buses
      |&gt; Enum.map(fn {b, offset} -&gt; rem(time + offset, b) end)

    if Enum.all?(mods, &amp;(&amp;1 == 0)) do
      time
    else
      # does too much work
      step =
        mods
        |&gt; Enum.with_index()
        # find indexes in mod list where the mod was 0
        |&gt; Enum.filter(fn {v, _i} -&gt; v == 0 end)
        # use the indexes to find the bus times that resulted in mod 0
        |&gt; Enum.map(fn {_v, i} -&gt; Enum.at(buses, i) |&gt; elem(0) end)
        |&gt; Enum.reduce(1, &amp;Kernel.*/2)

      loop(buses, time + step)
    end
  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="197352" 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-2020-day-13/36180/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-197352" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197352"
                     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="197354" data-post-id="197354">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I noticed the bus ids all being primes so lcm(x,y) == x*y. So basically I search through the remaining list of buses by incrementing the timestamp by a certain value, starting with the first bus’ id. Whenever one of the remaining buses fulfills the requirement, I multiply the incrementor by that bus’ id.<br>
Finds the timesramp very quickly.</p>
<p><a href="https://github.com/mruoss/advent_of_code_2020/blob/master/lib/aoc_2020/day13/solver.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/mruoss/advent_of_code_2020/blob/master/lib/aoc_2020/day13/solver.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="197354" 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-2020-day-13/36180/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-197354" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197354"
                     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="197375" data-post-id="197375">
  <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>Another tough one. I couldn’t solve it and decided not to dwell more than a day on each question. Turns out not having the star bothers me though <img src="https://forum.elixirforum.com/images/emoji/apple/roll_eyes.png?v=15" title=":roll_eyes:" class="emoji" alt=":roll_eyes:" loading="lazy" width="20" height="20">. Chinese remainder theory, eh? Will have to come back to this one after doing some reading (and avoid peeking at the answers above).</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>part1 - <strong>O(n)</strong></p>
<p>part2 - brute force with step by max_bus_id didnt print any result after ~1 hr <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"> - <a href="https://mathworld.wolfram.com/ChineseRemainderTheorem.html" rel="noopener nofollow ugc">chinese remainder theorem</a> (this is new for me) works only with prime numbers but the statement does not guarantee that bus-IDs will always be prime</p>
<p><strong>part 1</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day13 do

  def start(file \\ "/tmp/input.txt"), do: File.read!(file) |&gt; String.split(["\n", ","], trim: true) |&gt; find_bus() |&gt; elem(2)

  def find_bus([time|lst_bus_id]), do: Enum.reduce(lst_bus_id, {String.to_integer(time), -1, 0}, &amp;calculate_time/2)

  defp calculate_time("x", acc), do: acc
  defp calculate_time(id, acc) when is_binary(id), do: calculate_time(String.to_integer(id), acc)
  defp calculate_time(id, acc), do: calculate_time(id, acc, (id - rem(elem(acc, 0), id)))
  defp calculate_time(id, {time, min_wait_time, _best_result}, m) when m &lt; min_wait_time or min_wait_time &lt; 0, do: {time, m, id * m}
  defp calculate_time(_, acc, _), do: acc

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="197387" 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-2020-day-13/36180/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-197387" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197387"
                     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="197589" data-post-id="197589">
  <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>Gave up and used this after my brute force still didn’t complete after two hours. Now to just understand it…</p>
<p>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> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It starts off with zero busses, meaning t=0 is a valid solution, and the ‘step’ to get to the next valid solution is 1. That’s why the initial accumulator is <code>{0, 1}</code>.</p>
<p>Then it adds one bus at a time: it checks if the new bus leaves at required number of minutes after the current candidate t:</p>
<ul>
<li>
<p>If yes, it determines the new step value (the <a href="https://en.wikipedia.org/wiki/Least_common_multiple" rel="noopener nofollow ugc">LCM</a> of the previous step value and the current bus interval); it then moves on to the next bus.</p>
</li>
<li>
<p>If no, it moves t to the next time when all the previous busses line up and tries again</p>
</li>
</ul> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Part 2 was a real bear (especially before I twigged that some indices were <em>greater than</em> the bus line id!) until I hit on the “sieving” idea with increasing jumps, and then it became crystal clear, as the below <code>find_ordered_timestamp</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13 do
  @input  """
          1000001
          29,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,577,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,19,x,x,x,23,x,x,x,x,x,x,x,601,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37
          """
          |&gt; String.trim
          |&gt; String.split("\n")

  def part1() do
    [min_depart_str, ids_str] = @input
    min_depart = String.to_integer(min_depart_str)
    ids =
      ids_str
      |&gt; String.split(",")
      |&gt; Enum.reject(fn(x) -&gt; x == "x" end)
      |&gt; Enum.map(&amp;String.to_integer/1)
    {best_wait, best_id} =
      ids
      |&gt; Enum.map(&amp;(&amp;1 - rem(min_depart, &amp;1)))
      |&gt; Enum.zip(ids)
      |&gt; Enum.min_by(fn({wait,_id}) -&gt; wait end)
    best_wait * best_id
  end

  def part2() do
    pairs =
      @input
      |&gt; List.last
      |&gt; String.split(",")
      |&gt; Enum.map(&amp;(if &amp;1 == "x", do: &amp;1, else: String.to_integer(&amp;1)))
      |&gt; Enum.with_index
      |&gt; Enum.reject(fn(item) -&gt; elem(item, 0) == "x" end)
      |&gt; Enum.sort
      |&gt; Enum.reverse
    # set up the first one for instant success
    {line, index} = hd(pairs)
    find_ordered_timestamp(pairs, 1, line - index)
  end

  defp find_ordered_timestamp(pairs = [{line,index}|rest], jump, candidate) do
    if rem(candidate + index, line) == 0 do
      # try SAME candidate again Just In Case it also works for next line
      find_ordered_timestamp(rest, jump * line, candidate)
    else
      find_ordered_timestamp(pairs, jump, candidate + jump)
    end
  end
  defp find_ordered_timestamp([], _, candidate), do: candidate
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="197846" 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-2020-day-13/36180/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-197846" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197846"
                     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>