<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="379193" data-post-id="379193">
  <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/5298_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">
								<aside class="quote no-group" data-username="lud" data-post="5" data-topic="73513">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/48/14382_2.png" class="avatar"> lud:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp mirror?([a, a]), do: true
  defp mirror?([a, b, a, b]), do: true
  defp mirror?([a, b, c, a, b, c]), do: true
  defp mirror?([a, b, c, d, a, b, c, d]), do: true
  defp mirror?([a, b, c, d, e, a, b, c, d, e]), do: true
  defp mirror?(_), do: false
</code></pre>
</blockquote>
</aside>
<p>Ever heard of metaprogramming? <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"> Mine goes up to 12 repetitions and then pattern matches the string which should be technically faster (but I am not sure.)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="379193" 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-2025-day-2/73513/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-379193" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379193"
                     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="379211" data-post-id="379211">
  <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>Yeah of course, but I was in rush so actually I just wrote a loop to print the clauses and then pasted them into the module. Quick and dirty <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>Edit: well that actually counts as metaprogramming <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" 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="379211" data-batch-url="/posts/batch_likers">
                        3
                      </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-2025-day-2/73513/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-379211" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379211"
                     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="379214" data-post-id="379214">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Ben.Day02 do
  @moduledoc ~S"""
  # Gift Shop

  Identify the invalid product IDs
  """

  @doc ~S"""

  ### Example data

  11-22,95-115,998-1012,1188511880-1188511890,222220-222224, 1698522-1698528,446443-446449,38593856-38593862,565653-565659, 824824821-824824827,2121212118-2121212124

  - The ranges are separated by commas (,)
  - each range gives its first ID and last ID separated by a dash (-).

  We return an integer indicating the sum of all the invalid IDs found with a 'mirror' sequence like 1010 or 11 or 858858."

    iex&gt; part_one("priv/data/ben/day_02_example_data.txt")
    1_227_775_554

  """
  @spec part_one(String.t()) :: non_neg_integer
  def part_one("priv/" &lt;&gt; _rest_of_path = path_to_data) do
    path_to_data
    |&gt; stream_input()
    |&gt; Enum.reduce([], fn
      range, acc -&gt;
        [Enum.filter(range, &amp;evenly_repeated_sequence_of_digits?/1) | acc]
    end)
    |&gt; List.flatten()
    |&gt; Enum.sum()
  end

  defp evenly_repeated_sequence_of_digits?(digit) do
    half_digit_length = div(digit_length(digit), 2)
    divisor_and_modulo = trunc(:math.pow(10, half_digit_length))

    digit_of_even_length?(digit) &amp;&amp;
      # this compares left half of digit with even length of digits
      # to is right half of digits
      # no need to convert digit to any other type
      div(digit, divisor_and_modulo) == rem(digit, divisor_and_modulo)
  end

  defp digit_of_even_length?(digit), do: rem(digit_length(digit), 2) == 0
  defp digit_length(digit), do: trunc(:math.log10(digit) + 1)

  # ------------------------------------------------------------------------------
  @doc ~S"""
  Now, an ID is invalid if it is made only of some sequence of digits repeated at least twice. So, 12341234 (1234 two times), 123123123 (123 three times), 1212121212 (12 five times), and 1111111 (1 seven times) are all invalid IDs.

    iex&gt; part_two("priv/data/ben/day_02_example_data.txt")
    4_174_379_265
  """
  @spec part_two(String.t()) :: non_neg_integer
  def part_two("priv/" &lt;&gt; _rest_of_path = path_to_data) do
    path_to_data
    |&gt; stream_input()
    |&gt; Enum.reduce([], fn
      range, acc -&gt;
        [Enum.filter(range, &amp;evenly_repeated_or_entirely_repeated_sequence_of_digits?/1) | acc]
    end)
    |&gt; List.flatten()
    |&gt; Enum.sum()
  end

  defp evenly_repeated_or_entirely_repeated_sequence_of_digits?(digit),
    do: evenly_repeated_sequence_of_digits?(digit) || repeated_sequence_of_digits?(digit)

  defp repeated_sequence_of_digits?(digit),
    do:
      digit
      |&gt; Integer.to_string()
      # Starts with any number except 0
      # Has a repeating pattern of digits from [0-9]
      # Must end in a digit
      |&gt; String.match?(~r/^([1-9]+[0-9]*)\1+$/)

  # ------------------------------------------------------------------------------

  defp stream_input(input) do
    File.read!(input)
    |&gt; String.trim()
    |&gt; Stream.unfold(&amp;next_word/1)
    |&gt; Stream.map(&amp;String.split(&amp;1, "-"))
    |&gt; Stream.map(fn [left, right] -&gt; String.to_integer(left)..String.to_integer(right) end)
  end

  # The next_function for Stream.unfold
  defp next_word("") do
    # When the accumulator (remaining string) is empty, stop the stream
    nil
  end

  defp next_word(remaining_string) do
    case String.split(remaining_string, ",", parts: 2) do
      [word, rest] -&gt;
        # Yield the word, and use the 'rest' as the next accumulator
        {word, rest}

      [word] -&gt;
        # Yield the final word, and use an empty string "" as the next accumulator
        # which will cause the next call to return nil and stop the stream.
        {word, ""}
    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="379214" 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-2025-day-2/73513/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-379214" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379214"
                     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="379218" data-post-id="379218">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Got the answer, then went on to rewrite it faster…</p>
<p>Test with example data: Passed, what can go wrong?! <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"><br>
Final result: slightly too low <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>For those wanna figure out my mistake:</p>
<blockquote>
<p>part_one: 41294979841 in 286.98ms<br>
part_two: 66497614013 in 280.2ms</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2025.Solutions.Y25.Day02 do
  alias AoC.Input

  @max_id_length 10
  @max_args Integer.floor_div(@max_id_length, 2)

  def parse(input, _part) do
    Input.read!(input)
    |&gt; String.trim()
    |&gt; String.split(",")
  end

  def part_one(problem), do: solve(problem, :p1)
  def part_two(problem), do: solve(problem, :p2)

  def solve(problem, mode) do
    problem
    |&gt; Stream.map(fn x -&gt; String.split(x, "-") end)
    |&gt; Stream.map(fn [x, y] -&gt; String.to_integer(x)..String.to_integer(y) end)
    |&gt; Enum.flat_map(&amp;check_range(&amp;1, mode))
    |&gt; Enum.sum()
  end

  def check_range(range, mode), do: Enum.map(range, &amp;check_id(mode, Integer.digits(&amp;1)))

  # Some macro programming
  vars = Macro.generate_unique_arguments(@max_args, __MODULE__)
  var_sets = Enum.map(1..@max_args, &amp;Enum.take(vars, &amp;1))

  for set &lt;- var_sets do
    superset = set ++ set
    superset1 = set ++ set ++ set
    superset2 = set ++ set ++ set ++ set
    superset3 = set ++ set ++ set ++ set ++ set
    def check_id(_, unquote(superset) = idl), do: Integer.undigits(idl)
    def check_id(:p2, unquote(superset1) = idl), do: Integer.undigits(idl)
    def check_id(:p2, unquote(superset2) = idl), do: Integer.undigits(idl)
    def check_id(:p2, unquote(superset3) = idl), do: Integer.undigits(idl)
  end

  def check_id(_, _), do: 0
end
</code></pre>
<p>Note to self: examine the whole input file before writing code (I keep using the example data for it, but miss some tricks and optimizations)</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Tried to make it understandable.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Solution.Year2025.Day02 do
  import Enum, only: [map: 2, chunk_every: 2, any?: 1, sum: 1]

  def part1(input), do: scan(input, 2)
  def part2(input), do: scan(input, :number_length)

  def scan(input, stop_at) do
    sum(for {l, h} &lt;- parse(input), n &lt;- l..h, is_invalid(n, stop_at), do: n)
  end

  def parse(input) do
    input
    |&gt; String.trim()
    |&gt; String.split(",", trim: true)
    |&gt; map(fn r -&gt;
      [l, h] = String.split(r, "-")
      {String.to_integer(l), String.to_integer(h)}
    end)
  end

  def is_invalid(n, _stop_at) when n &lt;= 9, do: false

  def is_invalid(n, stop_at) do
    n_into_chars = n |&gt; to_string() |&gt; to_charlist()
    number_length = length(n_into_chars)
    test_until = if stop_at == :number_length, do: number_length, else: stop_at

    # Cut into all sizes of equal pieces (length must be divisible into equal size)
    for i &lt;- 2..test_until, rem(number_length, i) == 0 do
      n_into_chars |&gt; chunk_every(div(number_length, i)) |&gt; uniform_list?()
    end
    |&gt; any?()
  end

  # Alternative: def uniform_list?(l), do: length(Enum.uniq(l)) == 1
  def uniform_list?([_a]), do: true
  def uniform_list?([a, b | _r]) when a != b, do: false
  def uniform_list?([_a, b | r]), do: uniform_list?([b | r])
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="379223" 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-2025-day-2/73513/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-379223" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379223"
                     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="379244" data-post-id="379244">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>look, ma - no brute force!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Y2025.Day02 do
  def digits(a) do
    trunc(:math.floor(:math.log10(a))) + 1
  end

  def pow10(n), do: trunc(:math.pow(10, n))

  def multiplier(d, 2), do: pow10(d) + 1
  def multiplier(d, n), do: 1 + pow10(d) * multiplier(d, n - 1)

  def start_and_multiplier(a, n) do
    dig = digits(a)
    {d, m} = {div(dig, n), Integer.mod(dig, n)}

    if m == 0 do
      {div(a, pow10(dig - d)), multiplier(d, n)}
    else
      {pow10(d), multiplier(d + 1, n)}
    end
  end

  def invalid_ids(a, b, n) do
    {start, mult} = start_and_multiplier(a, n)
    max_start = pow10(digits(start))

    start..(max_start - 1)
    |&gt; Enum.reduce_while([], fn i, acc -&gt;
      x = i * mult

      cond do
        x &lt; a -&gt; {:cont, acc}
        x &gt; b -&gt; {:halt, Enum.reverse(acc)}
        true -&gt; {:cont, [x | acc]}
      end
    end)
  end

  def parse(s) do
    String.split(s, ",")
    |&gt; Enum.map(fn s -&gt;
      [a, b] = String.split(s, "-")
      [String.to_integer(a), String.to_integer(b)]
    end)
  end

  def part1(s) do
    parse(s)
    |&gt; Enum.flat_map(fn [a, b] -&gt; invalid_ids(a, b, 2) end)
    |&gt; Enum.sum()
  end

  def all_invalid_ids(a, b) do
    2..max(digits(b), 2)
    |&gt; Enum.flat_map(fn i -&gt; invalid_ids(a, b, i) end)
    |&gt; Enum.uniq()
  end

  def part2(s) do
    parse(s)
    |&gt; Enum.flat_map(fn [a, b] -&gt; all_invalid_ids(a, b) end)
    |&gt; Enum.sum()
  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="379244" data-batch-url="/posts/batch_likers">
                        3
                      </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-2025-day-2/73513/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-379244" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379244"
                     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="379275" data-post-id="379275">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Nice, this is the fastest solution I’ve tried. I’m just trying to wrap my head around the math in your <code>start_and_multiplier</code> function</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The idea is simple, and best illustrated with a few examples:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(2)&gt;  start_and_multiplier(998,2)
{10, 101}
iex(3)&gt;  start_and_multiplier(11,2)
{1, 11}
iex(4)&gt;  start_and_multiplier(12345,3)
{10, 10101}
iex(5)&gt;  start_and_multiplier(12345678,4)
{12, 1010101}
</code></pre>
<p>First start with an observation that repeating a 2 digits number, say 12, three times is the same  as multiplying it by 10101. That’s the <em>multiplier</em> part.</p>
<p>The <em>start</em> part is a bit trickier: if the digits of the number <code>a</code> can be divided neatly into <code>n</code> groups of digits, it’s the first such group:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(7)&gt;  start_and_multiplier(123456, 3)
{12, 10101}
iex(8)&gt;  start_and_multiplier(123456, 2)
{123, 1001}
</code></pre>
<p>Otherwise, we take the first <code>div(# digits in a, n)</code> digits and take the next power of 10 greater than that number, so e.g., 9 → 10, 23 → 100, etc. The reason is that for numbers like 998, the digits cannot be split evenly in two groups, so the first number we should start with is 1010 = 10 * 101.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And so the algorithm is, essentially: invalid ids are all the numbers from <code>[ start * multiplier, (start + 1) * multiplier, (start + 2) * multiplier, ...]</code> that belong to <code>a..b</code>. There is a trick to terminate the loop early, when incrementing <code>start</code> increases the number of digits, at which point <code>multiplier</code> won’t work anymore, and we implicitly use the fact that the difference between <code>a</code> and <code>b</code> is not too large - my algorithm won’t work for <code>a = 10, b = 100000</code>, for example.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2 do
  def file, do: Parser.read_file(2)
  def test, do: Parser.read_file("test")

  def parse(input) do
    input
    |&gt; Enum.join()
    |&gt; String.split(",")
    |&gt; Enum.map(&amp;(&amp;1 |&gt; String.split("-") |&gt; Enum.map(fn a -&gt; String.to_integer(a) end)))
  end

  def solve(input \\ file()) do
    input
    |&gt; parse()
    |&gt; Enum.reduce([], fn range, acc -&gt;
      range
      |&gt; find_invalid_ids_in_range(&amp;is_symmetric?/1)
      |&gt; Enum.concat(acc)
    end)
    |&gt; Enum.sum()
  end

  def solve_two(input \\ file()) do
    input
    |&gt; parse()
    |&gt; Enum.reduce([], fn range, acc -&gt;
      range
      |&gt; find_invalid_ids_in_range(&amp;contain_pattern?/1)
      |&gt; Enum.concat(acc)
    end)
    |&gt; Enum.sum()
  end

  def find_invalid_ids_in_range([alpha, omega], filter_function) do
    Enum.filter(alpha..omega, &amp;filter_function.(&amp;1))
  end

  def is_symmetric?(number) do
    string = Integer.to_string(number)
    length = String.length(string)
    {a, b} = String.split_at(string, (length / 2) |&gt; trunc())
    a == b
  end

  def contain_pattern?(number) do
    string = Integer.to_string(number)
    length = String.length(string)

    if length == 1 do
      false
    else
      divide_by = Enum.filter(2..length, fn a -&gt; rem(length, a) == 0 end)
      Enum.any?(divide_by, &amp;is_invalid_multiple(string, length, &amp;1))
    end
  end

  def is_invalid_multiple(string, length, divide_by) do
    slice_to = trunc(length / divide_by) - 1
    pattern = string |&gt; String.slice(0..slice_to)
    String.duplicate(pattern, divide_by) == string
  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="379288" 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-2025-day-2/73513/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-379288" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379288"
                     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/73513/load_more?page=3">Load more posts (3 remaining)</a>
</div></template></turbo-stream>