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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Brand new to Elixir, this is my solution to part 1. Happy to hear anything to make it more Elixir-y or performant, or even pointers on to what to read about for concepts to make this better.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp day1_process(filename) do
    File.read!(Path.absname(filename)) 
    |&gt; String.split("\n", trim: true) 
    |&gt; Enum.map(&amp;String.to_integer/1) 
    |&gt; Enum.filter(&amp;(&amp;1 &lt; 2020))
  end

  @doc """
  Advent of Code day 1 solution

  ## Examples
      iex&gt; AdventOfCode2020.day1()
  """
  def day1 do
    input = day1_process("lib/day_one_input.txt")
    Enum.map(input, &amp;(2020 - &amp;1)) 
    |&gt; Enum.filter(fn x -&gt; Enum.member?(input, x) end) 
    |&gt; Enum.reduce(&amp;(&amp;1 * &amp;2))
    
  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="196313" 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/33">Post #32</a>
	                </div>
	            </div>
              <div id="likers-container-196313" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196313"
                     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 #32"></div>
  </section>
</div>
    <div class="postbit" id="196318" data-post-id="196318">
  <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>You said you are brand new to Elixir, but your code doesn’t look like so. It looks clean and beautiful <img src="https://forum.elixirforum.com/images/emoji/apple/+1.png?v=15" title=":+1:" class="emoji" alt=":+1:" loading="lazy" width="20" height="20"></p>
<p>Pre-eliminating hopeless members is also interesting. It has the potential of optimizing the performance a little.</p>
<p>Your approach is a little bit not so straightforward to me, but I think it’s okay since the time complexity is still O(n * n).</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you so much! I’m really enjoying Elixir, it’s a really fun language to use so far. I use Pandas quite a bit in my day job, and I love its functional capabilities, so maybe that’s helping me ease into Elixir better <img src="https://forum.elixirforum.com/images/emoji/apple/grinning.png?v=15" title=":grinning:" class="emoji" alt=":grinning:" 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="196323" 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/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-196323" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196323"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="196325" data-post-id="196325">
  <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>The next step maybe to try parallelizing parts of your code? Just for fun <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"> The <code>Enum.map</code> part can be easily parallelized. So can the <code>Enum.filter</code> part. The <code>Enum.reduce</code> part is a bit tricky, but you can write your own <code>reduce</code> function that handles only <strong>associative</strong> operations (scalar multiplication is associative) and parallelize it.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Not as elegant as adamu’s solution with comprehensions, but it is about 33% faster, because it avoids checking duplicates. Constructing the map of %{index =&gt; number} uses up a lot of memory though: 20 times more according to my benchmark!</p>
<p>Just thought I’d share. <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"> Learned a lot from the solutions posted here, thanks!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @spec find_product([pos_integer()]) :: {:ok, pos_integer()} | {:error, String.t()}
  def find_product(numbers) do
    {_, number_map} = numbers
      |&gt; Enum.reduce({0, %{}}, fn(num, {index, acc}) -&gt;
        {index + 1, Map.put(acc, index, num)}
      end) # %{ 0 =&gt; num0, 1 =&gt; num1 }

    products = for index &lt;- (0..length(numbers) - 3),
      index2 &lt;- (index + 1..length(numbers) - 2),
      index3 &lt;- (index2 + 1..length(numbers) - 1),
      sum_is_2020?(number_map, {index, index2, index3}) do
        %{^index =&gt; num1, ^index2 =&gt; num2, ^index3 =&gt; num3} = number_map
        num1 * num2 * num3
    end

    case products do
      [product | _tail] -&gt; {:ok, product}
      [] -&gt; {:error, "No three numbers were found that add up to 2020"}
    end
  end

  @spec sum_is_2020?(%{pos_integer() =&gt; pos_integer()}, {pos_integer(), pos_integer(), pos_integer()}) :: boolean()
  defp sum_is_2020?(number_map, {a, b, c}) do
    %{^a =&gt; num1, ^b =&gt; num2, ^c =&gt; num3} = number_map
    num1 + num2 + num3 == 2020
  end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir"></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="196341" 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/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-196341" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196341"
                     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 #36"></div>
  </section>
</div>
    <div class="postbit" id="196380" data-post-id="196380">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I am just getting started with elixir.<br>
Here is my code for Day 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day1 do
  defp updateExpenseMap([], map) do
    map
  end

  defp updateExpenseMap([head | remainingList], map) do
    freq = Map.get(map, head, 0)
    updateExpenseMap(remainingList, Map.put(map, head, freq + 1))
  end

  defp takeInput() do
    File.read!("Day1.txt")
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(fn str -&gt;
      {n, _} = Integer.parse(str)
      n
    end)
  end

  defp multiplication([], _, _) do
    :error
  end

  defp multiplication([head | remainingList], map, sum) do
    if Map.has_key?(map, sum - head) do
      {:ok, {head, sum - head}}
    else
      multiplication(remainingList, map, sum)
    end
  end

  defp multiplicationV2([], _, _) do
    :error
  end

  defp multiplicationV2([head | remainingList], map, sum) do
    freq = Map.get(map, head)
    map = Map.put(map, head, freq - 1)

    case multiplication([head | remainingList], map, sum - head) do
      :error -&gt;
        map = Map.put(map, head, freq)
        multiplicationV2(remainingList, map, sum)

      {:ok, {n1, n2}} -&gt;
        {n1, n2, head}
    end
  end

  def main(part) do
    inputList = takeInput()
    map = updateExpenseMap(inputList, %{})
    # IO.puts("#{map}")
    case part do
      1 -&gt;
        {n1, n2} = multiplication(inputList, map, 2020)
        n1 * n2

      2 -&gt;
        {n1, n2, n3} = multiplicationV2(inputList, map, 2020)
        n1 * n2 * n3
    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="196380" 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/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-196380" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196380"
                     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 #37"></div>
  </section>
</div>
    <div class="postbit" id="196390" data-post-id="196390">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>As I have already announced in another thread, I take my time this year and finished day 1 yesterday during the hour when all my kids and my sick wife took a nap, and I actually had some time with the computer…</p>
<p><a href="https://gitlab.com/NobbZ/aoc_ex/-/blob/master/lib/y2020/d01.ex" class="onebox" target="_blank" rel="noopener nofollow">https://gitlab.com/NobbZ/aoc_ex/-/blob/master/lib/y2020/d01.ex</a></p>
<p>On my laptop, part 2 requires a full milliseconds since I did shortcutting the result on the first hit. It was ~1.5 before that.</p>
<p>Intuitively, I’d still say the algorithm is O(n³), where n is the length of the input… Though if its less than a second, I usually do not care…</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Looks like I’m a bit late to the party here <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"><br>
Some Erlang allowed? <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"><br>
First idea was to take each element and do the calculation to the rest of the list that comes after the element.<br>
Quite easy for part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">-module(day1_1).
-export([run/0]).

run()-&gt;
    process_list(load_file("day1input.txt")).

load_file(Filename)-&gt;
    {ok, Binary} = file:read_file(Filename),
    StringContent = unicode:characters_to_list(Binary),
    [ element(1, string:to_integer(Substr)) || Substr &lt;- string:tokens(StringContent, "\n")]. 

process_list([H | T])-&gt;
    case process_sublist(H, T) of
        notfound -&gt; process_list(T);
        {found, X, Y, Result} -&gt; {X, Y, Result}
    end;
process_list([])-&gt;
    notfound.

process_sublist(Elem, [H | T]) -&gt;
    case is_solution(Elem, H) of
        true -&gt; {found, Elem, H, H*Elem};
        false -&gt; process_sublist(Elem, T)
    end;
process_sublist(_, []) -&gt;
    notfound.

is_solution(X, Y) -&gt;
    X + Y =:= 2020.
</code></pre>
<p>but getting more complicated for part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">-module(day1_2).
-export([run/0]).

run()-&gt;
    process_list(load_file("day1input.txt")).

load_file(Filename)-&gt;
    {ok, Binary} = file:read_file(Filename),
    StringContent = unicode:characters_to_list(Binary),
    [ element(1, string:to_integer(Substr)) || Substr &lt;- string:tokens(StringContent, "\n")]. 

process_list([H | T])-&gt;
    case process_sublist(H, T) of
        notfound -&gt; process_list(T);
        X -&gt; X
    end;
process_list([])-&gt;
    notfound.

process_sublist(Elem, [H1 | T]) -&gt;
    case process_subsublist(Elem, H1, T) of
        notfound -&gt; process_sublist(Elem, T);
        X -&gt; X
    end;
process_sublist(_, []) -&gt;
    notfound.

process_subsublist(Elem1, Elem2, [H | T])-&gt;
    case is_solution(Elem1, Elem2, H) of
        true -&gt; {found, Elem1, Elem2, H, H*Elem1*Elem2};
        false -&gt; process_subsublist(Elem1, Elem2, T)
    end;
process_subsublist(_,_, []) -&gt;
    notfound.

is_solution(X, Y, Z) -&gt;
    X + Y + Z =:= 2020.
</code></pre>
<p>After finishing that I thought it’s just too much code, and did the brute force one-liner <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">process_list(L)-&gt;
    [{X, Y, X*Y} || X &lt;- L, Y &lt;- L, X + Y =:= 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="196456" 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/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-196456" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196456"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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