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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="egze" data-post="16" data-topic="35917">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/egze/48/13522_2.png" class="avatar"> egze:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">def part1(input \\ processed()) do
    [{x, y}] =
      Stream.flat_map(input, fn i -&gt;
        Stream.flat_map(input, fn j -&gt;
          [{i, j}]
        end)
      end)
      |&gt; Stream.filter(fn {x, y} -&gt;
        x + y == 2020
      end)
      |&gt; Enum.take(1)

    x * y
  end
</code></pre>
</blockquote>
</aside>
<p>That’s a super cool idea, but I think there is a bug in this code.<br>
The nested flat_maps do not kind of advance the stream by 1 if you know what I mean so they also produce tuples like:</p>
<p>{1721, 1721}</p>
<p>So If you add to the example <code>1010</code> on the first line, the result will be <code>1020100</code> which is wrong.</p>
<p>I fixed this by adding:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">      |&gt; Stream.filter(fn {x, y} -&gt;
        x + y == 2020 and x != y
      end)
</code></pre>
<p>This passes the example and the real input, but I think that it’s still not 100% correct because there could be duplicate expenses I think.</p>
<p>So if the input had</p>
<p>1010<br>
1010<br>
…<br>
…</p>
<p>Then <code>1020100</code> would be the correct answer I think.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196052" 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-1/35917/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-196052" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196052"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="196060" data-post-id="196060">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my take:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Events.Event2020.Day01 do
  def part_one(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; calc_depth(2)
  end

  def part_two(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; calc_depth(3)
  end

  def calc_depth(entries, max_depth),
    do: calc_depth_rec(entries, max_depth, 0, [])

  defp calc_depth_rec(_, max_depth, depth, terms)
       when depth &gt;= max_depth do
    sum = Enum.sum(terms)

    if sum == 2020 do
      Enum.reduce(terms, fn x, acc -&gt; x * acc end)
    else
      :not_found
    end
  end

  defp calc_depth_rec([], _max_depth, _depth, _terms) do
    :not_found
  end

  defp calc_depth_rec([entry | other_entries], max_depth, depth, terms) do
    case calc_depth_rec(other_entries, max_depth, depth + 1, [entry | terms]) do
      :not_found -&gt;
        calc_depth_rec(other_entries, max_depth, depth, terms)

      n -&gt;
        n
    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="196060" 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-1/35917/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-196060" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196060"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="196073" data-post-id="196073">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution which uses the fact that searching for <code>a + b + c == 2020</code> is the same as searching for <code>b + c == 2020 - a</code> and the fact that <code>a + b == b + a</code> and <code>a * b == b * a</code>, which mean that <code>[a, …, b]</code> will return the same result as <code>[b, …, a]</code> (in other words, if we didn’t found the solution for earlier elements in list, we never need to look at them anymore):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Solution do
  def read_input(path) do
    path
    |&gt; File.stream!()
    |&gt; Stream.map(&amp;String.to_integer(String.trim(&amp;1)))
    |&gt; Enum.to_list()
  end

  def run([], _, _), do: nil
  def run(_, _, sum) when sum &lt; 0, do: nil
  def run([a | _], 1, a), do: [a]

  def run([a | rest], n, sum) do
    case run(rest, n - 1, sum - a) do
      nil -&gt; run(rest, n, sum)
      nums when is_list(nums) -&gt; [a | nums]
    end
  end
end

list = Solution.read_input("input.txt")

list
|&gt; Solution.run(2, 2020)
|&gt; Enum.reduce(&amp;*/2)
|&gt; IO.inspect(label: "task 1")

list
|&gt; Solution.run(3, 2020)
|&gt; Enum.reduce(&amp;*/2)
|&gt; IO.inspect(label: "task 2")
</code></pre>
<p>Oh, this is also generic solution for any amount of elements that need to sum to given value.</p>
<p>I have benched against <a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a> code and this is result:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Number of Available Cores: 8
Available memory: 32 GB
Elixir 1.11.2
Erlang 22.3

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 21 s

Benchmarking first solution...
Benchmarking mine...
Benchmarking optimized...

Name                     ips        average  deviation         median         99th %
mine                  3.92 K      254.84 μs     ±7.99%      251.98 μs      348.98 μs
optimized             3.68 K      271.99 μs    ±10.21%      266.98 μs      397.98 μs
first solution      0.0200 K    49956.44 μs    ±12.46%    48000.98 μs    70571.10 μs

Comparison:
mine                  3.92 K
optimized             3.68 K - 1.07x slower +17.15 μs
first solution      0.0200 K - 196.03x slower +49701.60 μs
</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="196073" 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-2020-day-1/35917/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-196073" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196073"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="196077" data-post-id="196077">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That optimized list comprehension is fast but (assuming I’ve benchmarked correctly) I’m getting faster results with <code>Enum.member?</code> (aka <code>in</code>). Here’s my benchmarks using your input file:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Operating System: macOS
CPU Information: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Number of Available Cores: 16
Available memory: 16 GB
Elixir 1.11.1
Erlang 23.1.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 14 s

Benchmarking day1_enum_member...
Benchmarking day1_list_comp...

Name                       ips        average  deviation         median         99th %
day1_enum_member       25.30 K       39.53 μs    ±23.30%          36 μs          86 μs
day1_list_comp          6.56 K      152.45 μs    ±18.06%         139 μs         277 μs

Comparison: 
day1_enum_member       25.30 K
day1_list_comp          6.56 K - 3.86x slower +112.92 μs
</code></pre>
<p>And here’s the solution I’m using:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def run_enum_member([a | rest]), do: run_enum_member([a | rest], a, rest)
  def run_enum_member([_ | []], _, remaining), do: run_enum_member(remaining)

  def run_enum_member([b | rest], a, remaining) do
    target = 2020 - a - b

    cond do
      target &lt;= 0 -&gt; run_enum_member(rest, a, remaining)
      target in rest -&gt; {:ok, a * b * target}
      true -&gt; run_enum_member(rest, a, remaining)
    end
  end
</code></pre>
<p>Of course a lot of performance depends on where the three numbers show up in the list, no? Great fun seeing everyone’s 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="196077" 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-1/35917/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-196077" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196077"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="196099" data-post-id="196099">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Feels a little hacky, didn’t know how to break out early doing it this way.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Solutions.Day1 do
  alias Solutions.Helpers

  def part_1(expenses) do
    expenses = Enum.map(expenses, &amp;String.to_integer/1)

    sums_to_2020? = fn i, j -&gt; i + j == 2020 end

    answers = for i &lt;- expenses, j &lt;- expenses, sums_to_2020?.(i, j), do: {i, j}
    {i, j} = List.first(answers)
    i * j
  end

  def part_2(expenses) do
    expenses = Enum.map(expenses, &amp;String.to_integer/1)

    sums_to_2020? = fn i, j, k -&gt; i + j + k == 2020 end

    answers =
      for i &lt;- expenses, j &lt;- expenses, k &lt;- expenses, sums_to_2020?.(i, j, k), do: {i, j, k}

    {i, j, k} = List.first(answers)
    i * j * k
  end

  def answer do
    expenses = Helpers.load_list_file("priv/day_1_input.txt")
    part_1 = part_1(expenses)
    part_2 = part_2(expenses)

    IO.inspect(part_1)
    IO.inspect(part_2)
  end
end
</code></pre>
<p>full code: <a href="https://gitlab.com/agundy/advent_of_code/-/blob/master/2020/solutions/lib/solutions/day_1.ex" class="inline-onebox" rel="noopener nofollow ugc">2020/solutions/lib/solutions/day_1.ex · master · Aaron Gunderson / advent_of_code · GitLab</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="196099" 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-1/35917/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-196099" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196099"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="196100" data-post-id="196100">
  <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><strong>Part 1:</strong></p>
<p>Big-O</p>
<blockquote>
<p>Sort → O(n log n)<br>
Find → O(n)</p>
</blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day1 do

  @lucky_num 2020

  def part1(arr), do:
    arr |&gt; Enum.sort() |&gt; find()

  defp find(lst) when length(lst) &lt; 2, do: {:error, "not found"}
  defp find([first|tail]) do
    {lst,[last]} = Enum.split(tail, length(tail) - 1)
    find(first, last, lst, tail)
  end

  defp find(first, last, _lst, _tail) when first + last == @lucky_num, do: first * last
  defp find(first, last, lst, _tail) when first + last &gt; @lucky_num, do: find([first | lst])
  defp find(_first, _last, _lst, tail), do: find(tail)

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="196100" 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-1/35917/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-196100" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196100"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="196107" data-post-id="196107">
  <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>Part2</p>
<p><em>2 b improved: have a feeling that with one more accumulator can speed up the solution</em></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day1b do

  @lucky_num 2020

  def part2(arr), do:
    arr |&gt; Enum.sort() |&gt; find()

  defp find(lst) when length(lst) &lt; 3, do: {:error, "not found"}
  defp find([first,second|tail]) do
    {lst,[last]} = Enum.split(tail, length(tail) - 1)
    find(first, second, last, lst, tail)
  end

  defp find(first, second, last, _lst, _tail) when first + second + last == @lucky_num, do: first * second * last
  defp find(first, second, last, lst, _tail) when first + second + last &gt; @lucky_num, do: find([first, second] ++ lst)
  defp find(first, second, _last, _lst, tail) do
    case find([second | tail]) do
      {:error, _reason} -&gt; find([first | tail])
      v -&gt; v
    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="196107" 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-1/35917/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-196107" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196107"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="196109" data-post-id="196109">
  <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>Still just getting my feet wet with Elixir. Certainly not optimized but for the presented data set it’s efficient enough on my machine.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def parse_input() do
    {:ok, expenses} = File.read("lib/input.txt")

    expenses
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;String.to_integer(&amp;1))
  end

  def find_two([h | t], sum) do
    case Enum.find(t, fn x -&gt; sum - h == x end) do
      nil -&gt;
        find_two(t, sum)

      y -&gt;
        h * y
    end
  end

  def find_two([], _sum) do
    nil
  end

  def find_two(sum) do
    parse_input()
    |&gt; find_two(sum)
  end

  def find_three([], _sum) do
    nil
  end

  def find_three([h | t], sum) do
    case find_two(t, sum - h) do
      nil -&gt; find_three(t, sum)
      y -&gt; h * y
    end
  end

  def find_three(sum) do
    parse_input()
    |&gt; find_three(sum)
  end
end

IO.puts(SumMultiply.find_two(2020))
IO.puts(SumMultiply.find_three(2020))

</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="196109" 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-1/35917/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-196109" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196109"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="196157" data-post-id="196157">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my version with early break out of comprehension.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Event1 do
  def run do
    IO.puts("Test part1: #{part1("input/event1/test.txt")}")
    IO.puts("Puzzle part1: #{part1("input/event1/puzzle.txt")}")
    IO.puts("Test part2: #{part2("input/event1/test.txt")}")
    IO.puts("Puzzle part2: #{part2("input/event1/puzzle.txt")}")
  end

  def part1(path), do: input_stream(path) |&gt; Enum.into([]) |&gt; find_result()
  def part2(path), do: input_stream(path) |&gt; Enum.into([]) |&gt; find_result2()

  def input_stream(path), do: path |&gt; File.stream!() |&gt; Stream.map(&amp;parse_input/1)
  def parse_input(input), do: input |&gt; Integer.parse() |&gt; elem(0)

  def find_result(inputs) do
    try do
      for(i &lt;- inputs, j &lt;- inputs, i + j == 2020, do: throw(i * j))
    catch
      x -&gt; x
    end
  end

  def find_result2(inputs) do
    try do
      for(i &lt;- inputs, j &lt;- inputs, k &lt;- inputs, i + j + k == 2020, do: throw(i * j * k))
    catch
      x -&gt; x
    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="196157" 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-1/35917/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-196157" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196157"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="196211" data-post-id="196211">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Aetherus" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Aetherus/120/17203_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Aetherus
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><strong>Revisit Day 1</strong></p>
<p>I’ve played a little bit with the <code>Stream</code> module and managed to rewrite my previous solution in a streamable fashion.</p>
<p>The idea came from Ruby’s <code>Array#combination</code> which returns an <code>Enumerator</code> instance if no block is given. If Ruby can do that, why can’t Elixir?</p>
<p><strong>WARNING:</strong> EXTREMELY SLOW!!!</p>
<p>Here’s my code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Combination do

  @doc """
  Creates a stream that iterates through 
  all the k-element combinations of the given enumerable.
  """
  @spec c(Enumerable.t(), pos_integer) :: Enumerable.t()
  def c(enum, 1) do
    Stream.map(enum, &amp;[&amp;1])
  end

  def c(enum, k) when is_integer(k) and k &gt; 1 do
    Stream.transform(enum, Stream.drop(enum, 1), fn item, acc -&gt;
      next = acc
             |&gt; c(k - 1)
             |&gt; Stream.map(&amp;[item|&amp;1])
      new_acc = Stream.drop(acc, 1)
      {next, new_acc}
    end)
  end
end

nums = "./day1.txt"
       |&gt; File.stream!([], :line)
       |&gt; Stream.map(&amp;String.trim/1)
       |&gt; Stream.map(&amp;String.to_integer/1)

solve = fn(k)-&gt;
  nums
  |&gt; Combination.c(k)
  |&gt; Enum.find(&amp;(Enum.sum(&amp;1) == 2020))
  |&gt; Enum.reduce(&amp;*/2)
  |&gt; IO.puts()
end

solve.(2)
solve.(3)
</code></pre>
<p>I feel the time complexity of the stream approach is quite hard for me to assess, while the time complexity of the callback approach is clearly O(combination(n, k)).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196211" 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-1/35917/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-196211" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196211"
                     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 #31"></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/35917/load_more?page=4">Load more posts (8 remaining)</a>
</div></template></turbo-stream>