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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s mine for today, much easier than yesterday, with some simple optimisations got it down to.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Solution for 2024 day 7
part_one: 12553187650171 in 6.33ms
part_two: 96779702119491 in 71.4ms
</code></pre>
<p>Not at all happy with the conditional logic/code duplication, so might take some time to tidy that up later, but it works at least!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
    defmodule Aoc2024.Solutions.Y24.Day07 do
      alias AoC.Input

      def parse(input, _part) do
        Input.read!(input)
        |&gt; String.split("\n", trim: true)
        |&gt; Enum.map(fn ip -&gt;
          [total | options] = String.split(ip, " ")
          [total | _] = String.split(total, ":")
          {total |&gt; String.to_integer(), options |&gt; Enum.map(&amp;String.to_integer/1)}
        end)
      end

      def part_one(problem) do
        problem
        |&gt; split_into_chunks()
        |&gt; Task.async_stream(&amp;do_calculations/1)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(fn {_total, valid} -&gt;
          valid
        end)
        |&gt; Enum.reduce(0, fn {total, _}, acc -&gt;
          acc + total
        end)
      end

      def do_calculations(problem) do
        problem
        |&gt; Enum.map(fn {total, [current | rest]} -&gt;
          {total, do_calculation(total, rest, current)}
        end)
      end

      def do_calculation(total, [], current) do
        [current == total]
      end

      def do_calculation(total, [option | rest], current) do
        multi_valid = check_multi_calculation(total, option, current)
        add_valid = check_plus_calculation(total, option, current)

        cond do
          multi_valid and add_valid -&gt;
            [
              do_calculation(total, rest, current * option),
              do_calculation(total, rest, current + option)
            ]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          multi_valid -&gt;
            [do_calculation(total, rest, current * option)]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          add_valid -&gt;
            [do_calculation(total, rest, current + option)]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          true -&gt;
            false
        end
      end

      def check_multi_calculation(total, option, current)
          when option * current &lt;= total do
        true
      end

      def check_multi_calculation(_total, _options, _current), do: false

      def check_plus_calculation(total, option, current)
          when option + current &lt;= total do
        true
      end

      def check_plus_calculation(_total, _options, _current), do: false

      def check_concat_calculation(total, option, current) do
        concat_number = current * trunc(:math.pow(10, trunc(:math.log10(option)) + 1)) + option

        if concat_number &lt;= total do
          {true, concat_number}
        else
          {false, 0}
        end
      end

      def part_two(problem) do
        problem
        |&gt; split_into_chunks()
        # |&gt; do_calculations_part_2()
        |&gt; Task.async_stream(fn options -&gt;
          do_calculations_part_2(options)
        end)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(fn {_total, valid} -&gt;
          valid
        end)
        |&gt; Enum.reduce(0, fn {total, _}, acc -&gt;
          acc + total
        end)
      end

      def do_calculations_part_2(problem) do
        problem
        |&gt; Enum.map(fn {total, [current | rest]} -&gt;
          {total, do_calculation_part_2(total, rest, current)}
        end)
      end

      defp split_into_chunks(options) do
        workers = :erlang.system_info(:schedulers_online)
        options_count = Enum.count(options)
        options_per_chunk = :erlang.ceil(options_count / workers)

        Enum.chunk_every(options, options_per_chunk)
      end

      defp merge_results_stream(results_stream) do
        Enum.reduce(results_stream, [], fn {:ok, worker_result}, acc -&gt;
          acc ++ worker_result
        end)
      end

      def do_calculation_part_2(total, [], current) do
        [current == total]
      end

      def do_calculation_part_2(total, [option | rest], current) do
        multi_valid = check_multi_calculation(total, option, current)
        add_valid = check_plus_calculation(total, option, current)
        {concat_valid, concat_value} = check_concat_calculation(total, option, current)

        cond do
          multi_valid and add_valid and concat_valid -&gt;
            [
              do_calculation_part_2(total, rest, current * option),
              do_calculation_part_2(total, rest, current + option),
              do_calculation_part_2(total, rest, concat_value)
            ]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          multi_valid and concat_valid -&gt;
            [
              do_calculation_part_2(total, rest, current * option),
              do_calculation_part_2(total, rest, concat_value)
            ]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          multi_valid and add_valid -&gt;
            [
              do_calculation_part_2(total, rest, current * option),
              do_calculation_part_2(total, rest, current + option)
            ]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          add_valid and concat_valid -&gt;
            [
              do_calculation_part_2(total, rest, current + option),
              do_calculation_part_2(total, rest, concat_value)
            ]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          multi_valid -&gt;
            [do_calculation_part_2(total, rest, current * option)]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          add_valid -&gt;
            [do_calculation_part_2(total, rest, current + option)]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          concat_valid -&gt;
            [do_calculation_part_2(total, rest, concat_value)]
            |&gt; List.flatten()
            |&gt; Enum.filter(&amp; &amp;1)
            |&gt; List.first(false)

          true -&gt;
            false
        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="348848" 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-7/67938/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-348848" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348848"
                     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="348849" data-post-id="348849">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Did some refactoring and <code>Task</code> work so I got it down to sub 100ms</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC.Y2024.Day7 do
  @moduledoc false

  use AOC.Solution

  @impl true
  def load_data() do
    Data.load_day(2024, 7)
    |&gt; Enum.map(fn line -&gt; String.split(line, ": ") end)
    |&gt; Enum.map(fn [test_value, numbers] -&gt;
      {String.to_integer(test_value),
       numbers |&gt; String.split(" ") |&gt; Enum.map(&amp;String.to_integer/1)}
    end)
  end

  @impl true
  def part_one(data) do
    solve(data, false)
  end

  @impl true
  def part_two(data) do
    solve(data, true)
  end

  defp solve(data, has_concat) do
    data
    |&gt; Enum.chunk_every(20)
    |&gt; Task.async_stream(fn chunk -&gt;
      chunk
      |&gt; Enum.filter(fn {test_value, numbers} -&gt;
        form_test_value?(numbers, test_value, has_concat)
      end)
      |&gt; General.map_sum(fn {test_value, _} -&gt; test_value end)
    end)
    |&gt; Enum.reduce(0, fn {:ok, res}, acc -&gt; acc + res end)
  end

  defp form_test_value?([acc | _], test_value, _) when acc &gt; test_value, do: false
  defp form_test_value?([test_value], test_value, _), do: true
  defp form_test_value?([_], _, _), do: false

  defp form_test_value?([a | [b | rest]], test_value, false) do
    form_test_value?([a * b | rest], test_value, false) or
      form_test_value?([a + b | rest], test_value, false)
  end

  defp form_test_value?([a | [b | rest]], test_value, true) do
    form_test_value?([a * b | rest], test_value, true) or
      form_test_value?([a + b | rest], test_value, true) or
      form_test_value?([String.to_integer("#{a}#{b}") | rest], test_value, true)
  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="348849" 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-7/67938/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-348849" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348849"
                     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="348850" data-post-id="348850">
  <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>Slow and naive, using a <a href="https://github.com/lud/adventofcode/blob/main/lib/advent_of_code/combinations.ex" rel="noopener nofollow ugc">stream of all possible combinations of operators</a>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Solutions.Y24.Day07 do
  alias AdventOfCode.Combinations
  alias AoC.Input

  def parse(input, _part) do
    Input.stream!(input, trim: true) |&gt; Enum.map(&amp;parse_line/1)
  end

  defp parse_line(line) do
    [result, operation] = String.split(line, ":")

    operands =
      operation
      |&gt; String.split(" ", trim: true)
      |&gt; Enum.map(&amp;String.to_integer/1)

    {String.to_integer(result), operands}
  end

  def part_one(problem) do
    solve(problem, [:*, :+])
  end

  def part_two(problem) do
    solve(problem, [:*, :+, :||])
  end

  defp solve(problem, operators) do
    problem
    |&gt; Enum.filter(&amp;can_be_computed?(&amp;1, operators))
    |&gt; Enum.map(&amp;elem(&amp;1, 0))
    |&gt; Enum.sum()
  end

  defp can_be_computed?({result, operands}, operators) do
    operators_combins = Combinations.of(operators, length(operands) - 1)
    Enum.any?(operators_combins, fn c -&gt; result == compute(operands, c) end)
  end

  defp compute([h | operands], operators) do
    Enum.reduce(Enum.zip(operators, operands), h, fn
      {:+, n}, acc -&gt; acc + n
      {:*, n}, acc -&gt; acc * n
      {:||, n}, acc -&gt; cat(acc, n)
    end)
  end

  defp cat(a, b) do
    Integer.undigits(Integer.digits(a) ++ Integer.digits(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="348850" 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-7/67938/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-348850" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348850"
                     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="348851" data-post-id="348851">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><strong>Edit 1</strong></p>
<p>Final optimised version for me, pretty happy with this.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    defmodule Aoc2024.Solutions.Y24.Day07 do
      alias AoC.Input

      def parse(input, _part) do
        Input.read!(input)
        |&gt; String.split("\n", trim: true)
        |&gt; Enum.map(fn ip -&gt;
          [total | options] = String.split(ip, " ")
          [total | _] = String.split(total, ":")
          {total |&gt; String.to_integer(), options |&gt; Enum.map(&amp;String.to_integer/1)}
        end)
      end

      def part_one(problem) do
        do_work(problem, :part_one)
      end

      def part_two(problem) do
        do_work(problem, :part_two)
      end

      def do_work(problem, part) do
        problem
        |&gt; split_into_chunks()
        |&gt; Task.async_stream(fn options -&gt;
          do_calculations(options, part)
        end)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(fn {_total, valid} -&gt;
          valid
        end)
        |&gt; Enum.reduce(0, fn {total, _}, acc -&gt;
          acc + total
        end)
      end

      def do_calculations(problem, part) do
        problem
        |&gt; Enum.map(fn {total, [current | rest]} -&gt;
          {total, do_calculation(total, rest, current, part)}
        end)
      end

      def do_calculation(total, _, current, _part) when current &gt; total do
        false
      end

      def do_calculation(total, [], current, _part) do
        current == total
      end

      def do_calculation(total, [option | rest], current, part) do
        case part do
          :part_one -&gt;
            do_calculation(total, rest, current * option, part) ||
              do_calculation(total, rest, current + option, part)

          :part_two -&gt;
            concat_value = get_concat_calculation(option, current)

            do_calculation(total, rest, current * option, part) ||
              do_calculation(total, rest, current + option, part) ||
              do_calculation(total, rest, concat_value, part)
        end
      end

      def get_concat_calculation(option, current) do
        current * trunc(:math.pow(10, trunc(:math.log10(option)) + 1)) + option
      end

      defp split_into_chunks(options) do
        workers = :erlang.system_info(:schedulers_online)
        options_count = Enum.count(options)
        options_per_chunk = :erlang.ceil(options_count / workers)

        Enum.chunk_every(options, options_per_chunk)
      end

      defp merge_results_stream(results_stream) do
        Enum.reduce(results_stream, [], fn {:ok, worker_result}, acc -&gt;
          acc ++ worker_result
        end)
      end
    end

</code></pre>
<p><strong>Original</strong></p>
<p>Slightly updated version removing the lists to accumulate results, realised I could just return a bool, doh! Still struggle sometimes with following the recursion login, more Coffee needed.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Solution for 2024 day 7
part_one: 12553187650171 in 4.05ms
part_two: 96779702119491 in 36.17ms
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">
    defmodule Aoc2024.Solutions.Y24.Day07 do
      alias AoC.Input

      def parse(input, _part) do
        Input.read!(input)
        |&gt; String.split("\n", trim: true)
        |&gt; Enum.map(fn ip -&gt;
          [total | options] = String.split(ip, " ")
          [total | _] = String.split(total, ":")
          {total |&gt; String.to_integer(), options |&gt; Enum.map(&amp;String.to_integer/1)}
        end)
      end

      def part_one(problem) do
        problem
        |&gt; split_into_chunks()
        |&gt; Task.async_stream(&amp;do_calculations/1)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(fn {_total, valid} -&gt;
          valid
        end)
        |&gt; Enum.reduce(0, fn {total, _}, acc -&gt;
          acc + total
        end)
      end

      def do_calculations(problem) do
        problem
        |&gt; Enum.map(fn {total, [current | rest]} -&gt;
          {total, do_calculation(total, rest, current)}
        end)
      end

      def do_calculation(total, [], current) do
        current == total
      end

      def do_calculation(total, [option | rest], current) do
        multi_valid = check_multi_calculation(total, option, current)
        add_valid = check_plus_calculation(total, option, current)

        cond do
          multi_valid and add_valid -&gt;
            do_calculation(total, rest, current * option) ||
              do_calculation(total, rest, current + option)

          # |&gt; List.flatten()
          # |&gt; Enum.filter(&amp; &amp;1)
          # |&gt; List.first(false)

          multi_valid -&gt;
            do_calculation(total, rest, current * option)

          # |&gt; List.flatten()
          # |&gt; Enum.filter(&amp; &amp;1)
          # |&gt; List.first(false)

          add_valid -&gt;
            do_calculation(total, rest, current + option)

          # |&gt; List.flatten()
          # |&gt; Enum.filter(&amp; &amp;1)
          # |&gt; List.first(false)

          true -&gt;
            false
        end
      end

      def check_multi_calculation(total, option, current)
          when option * current &lt;= total do
        true
      end

      def check_multi_calculation(_total, _options, _current), do: false

      def check_plus_calculation(total, option, current)
          when option + current &lt;= total do
        true
      end

      def check_plus_calculation(_total, _options, _current), do: false

      def check_concat_calculation(total, option, current) do
        concat_number = current * trunc(:math.pow(10, trunc(:math.log10(option)) + 1)) + option

        if concat_number &lt;= total do
          {true, concat_number}
        else
          {false, 0}
        end
      end

      def part_two(problem) do
        problem
        |&gt; split_into_chunks()
        # |&gt; do_calculations_part_2()
        |&gt; Task.async_stream(fn options -&gt;
          do_calculations_part_2(options)
        end)
        |&gt; merge_results_stream()
        |&gt; Enum.filter(fn {_total, valid} -&gt;
          valid
        end)
        |&gt; Enum.reduce(0, fn {total, _}, acc -&gt;
          acc + total
        end)
      end

      def do_calculations_part_2(problem) do
        problem
        |&gt; Enum.map(fn {total, [current | rest]} -&gt;
          {total, do_calculation_part_2(total, rest, current)}
        end)
      end

      defp split_into_chunks(options) do
        workers = :erlang.system_info(:schedulers_online)
        options_count = Enum.count(options)
        options_per_chunk = :erlang.ceil(options_count / workers)

        Enum.chunk_every(options, options_per_chunk)
      end

      defp merge_results_stream(results_stream) do
        Enum.reduce(results_stream, [], fn {:ok, worker_result}, acc -&gt;
          acc ++ worker_result
        end)
      end

      def do_calculation_part_2(total, [], current) do
        current == total
      end

      def do_calculation_part_2(total, [option | rest], current) do
        multi_valid = check_multi_calculation(total, option, current)
        add_valid = check_plus_calculation(total, option, current)
        {concat_valid, concat_value} = check_concat_calculation(total, option, current)

        cond do
          multi_valid and add_valid and concat_valid -&gt;
            do_calculation_part_2(total, rest, current * option) ||
              do_calculation_part_2(total, rest, current + option) ||
              do_calculation_part_2(total, rest, concat_value)

          multi_valid and concat_valid -&gt;
            do_calculation_part_2(total, rest, current * option) ||
              do_calculation_part_2(total, rest, concat_value)

          multi_valid and add_valid -&gt;
            do_calculation_part_2(total, rest, current * option) ||
              do_calculation_part_2(total, rest, current + option)

          add_valid and concat_valid -&gt;
            do_calculation_part_2(total, rest, current + option) ||
              do_calculation_part_2(total, rest, concat_value)

          multi_valid -&gt;
            do_calculation_part_2(total, rest, current * option)

          add_valid -&gt;
            do_calculation_part_2(total, rest, current + option)

          concat_valid -&gt;
            do_calculation_part_2(total, rest, concat_value)

          true -&gt;
            false
        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="348851" 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-7/67938/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-348851" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348851"
                     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="348852" data-post-id="348852">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is the first time I’m contributing with my solution here. I mostly want to say how I misrepresented the second part. For some reason, I assumed that concatenation would have precedence over the other operators. That complicated things quite a bit and only after implementing the solution, I realized that it didn’t match the example.</p>
<p>Anyway, here’s the (much simpler) working code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC2024.Day7 do
  def sum_valid(input, opts \\ [concat: false]) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;parse_equation/1)
    |&gt; Enum.filter(&amp;valid?(&amp;1, opts[:concat]))
    |&gt; Enum.map(fn {result, _numbers} -&gt; result end)
    |&gt; Enum.sum()
  end
  
  def parse_equation(text) do
    [result, number_text] = String.split(text, ": ")
    numbers =
      number_text
      |&gt; String.split()
      |&gt; Enum.map(&amp;String.to_integer/1)
    
    {String.to_integer(result), numbers}
  end

  def valid?({result, [h | t]}, concat) do
    valid?(t, result, h, concat)
  end

  defp valid?([], result, current, _), do: result == current
  defp valid?(_, result, current, _) when current &gt; result, do: false
  defp valid?([h | t], result, current, concat) do
    valid?(t, result, current + h, concat) or
      valid?(t, result, current * h, concat) or
      concat and valid?(t, result, concatenate(current, h), concat)
  end

  defp concatenate(a, b) do
    exponent = Integer.digits(b) |&gt; length()
    10 ** exponent * 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="348852" 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-7/67938/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-348852" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348852"
                     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="348853" data-post-id="348853">
  <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">
								<aside class="quote no-group" data-username="Sorc96" data-post="16" data-topic="67938">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sorc96/48/28361_2.png" class="avatar"> Sorc96:</div>
<blockquote>
<p>For some reason, I assumed that concatenation would have precedence over the other operators</p>
</blockquote>
</aside>
<p>Me too! I don’t know why, the text is clear enough <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>Anyway without using streams and using the 10/100/1000 integer shifting I now have a 40ms solution which is good enough <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="348853" 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-7/67938/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-348853" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348853"
                     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="348855" data-post-id="348855">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sevenseacat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sevenseacat/120/23153_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sevenseacat
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Ash Framework</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m amazed at the integer shifting for implementing the concatenation operator, and how much of a difference it makes.</p>
<p>Now mine runs in like 65ms!</p>
<p>(I don’t use streams but I always reach for <code>Task.async_stream</code> whenever I see a process that needs to be done for lots of different things)</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Nothing fancy today, my solution looks like some others :</p>
<pre data-code-wrap="ex"><code class="lang-ex">defmodule Y2024.D07 do
  use Day, input: "2024/07", part1: ~c"l", part2: ~c"l"

  defp part1(input), do: partX(input, false)
  defp part2(input), do: partX(input, true)

  defp partX(input, concat?) do
    input
    |&gt; Enum.map(&amp;parse_line/1)
    |&gt; Enum.filter(&amp;works?(&amp;1, concat?))
    |&gt; Enum.map(&amp;elem(&amp;1, 0))
    |&gt; Enum.sum()
  end

  defp works?({r, [a, b | t]}, concat?) do
    works?({r, [a + b | t]}, concat?) or
      works?({r, [a * b | t]}, concat?) or
      (concat? and works?({r, [concat(a, b) | t]}, concat?))
  end

  defp works?({r, [r]}, _), do: true
  defp works?(_, _), do: false

  defp concat(a, b) do
    b
    |&gt; Integer.digits()
    |&gt; Enum.count()
    |&gt; then(&amp;(10 ** &amp;1))
    |&gt; Kernel.*(a)
    |&gt; Kernel.+(b)
  end

  # defp parse_line...
end
</code></pre>
<p>To get the shift, <code>b |&gt; Integer.digits() |&gt; Enum.count()</code> give slightly faster results than <code>(b |&gt; :math.log10()  |&gt; floor()) + 1</code>.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I updated my <a href="https://forum.elixirforum.com/t/advent-of-code-2024-day-7/67938/11" rel="nofollow">solution</a> with <code>Task.async_stream</code> to reach 20ms on part 2 <img src="https://forum.elixirforum.com/images/emoji/apple/high_voltage.png?v=15" title=":high_voltage:" class="emoji" alt=":high_voltage:" 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="348861" 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-7/67938/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-348861" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348861"
                     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="348862" data-post-id="348862">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I should try this.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day07 do
  alias AoC.Input

  def parse(input, _part) do
    input
    |&gt; Input.stream!(trim: true)
    |&gt; Enum.map(fn line -&gt;
      [result, equation] = String.split(line, ":")
      equation_parts = equation |&gt; String.trim() |&gt; String.split(" ")
      {String.to_integer(result), Enum.map(equation_parts, &amp;String.to_integer/1)}
    end)
  end

  def part_one(problem) do
    Enum.reduce(problem, 0, fn {result, parts}, acc -&gt;
      if valid_equation?(parts, [:+, :*], result), do: result + acc, else: acc
    end)
  end

  def part_two(problem) do
    {solution, problem} =
      Enum.reduce(problem, {0, []}, fn {result, parts}, {solution, next_parts} -&gt;
        if valid_equation?(parts, [:+, :*], result),
          do: {solution + result, next_parts},
          else: {solution, [{result, parts} | next_parts]}
      end)

    Enum.reduce(problem, solution, fn {result, parts}, acc -&gt;
      if valid_equation?(parts, [:+, :*, :||], result), do: result + acc, else: acc
    end)
  end

  defp valid_equation?([part | rest_of_parts], operators, result) do
    length(rest_of_parts)
    |&gt; generate_operator_combinations(operators)
    |&gt; Enum.reduce_while(false, fn combination, acc -&gt;
      if calculate_equation(combination, rest_of_parts, part) == result do
        {:halt, true}
      else
        {:cont, acc}
      end
    end)
  end

  defp generate_operator_combinations(length, operators) do
    Enum.reduce(1..length, [[]], fn _, acc -&gt;
      for symbol &lt;- operators,
          combination &lt;- acc do
        [symbol | combination]
      end
    end)
  end

  defp calculate_equation(_, [], solution), do: solution

  defp calculate_equation([:+ | operators], [number | numbers], solution) do
    calculate_equation(operators, numbers, solution + number)
  end

  defp calculate_equation([:* | operators], [number | numbers], solution) do
    calculate_equation(operators, numbers, solution * number)
  end

  defp calculate_equation([:|| | operators], [number | numbers], solution) do
    calculate_equation(operators, numbers, String.to_integer("#{solution}" &lt;&gt; "#{number}"))
  end
end
</code></pre>
<p>Part 2 is slow. <img src="https://forum.elixirforum.com/images/emoji/apple/disappointed.png?v=15" title=":disappointed:" class="emoji" alt=":disappointed:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Solution for 2024 day 7
part_one: 5540634308362 in 56.43ms
part_two: 472290821152397 in 16.31s
</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="348862" 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-7/67938/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-348862" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348862"
                     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/67938/load_more?page=3">Load more posts (29 remaining)</a>
</div></template></turbo-stream>