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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mudasobwa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudasobwa/120/5298_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mudasobwa
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Cure</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wonder why nearly all the solutions do <code>String.split/2</code> when we can directly pattern-match strings</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp d2_destination_reducer("forward " &lt;&gt; value, acc),
    do: %{acc | h: acc.h + String.to_integer(value)}

  defp d2_destination_reducer("down " &lt;&gt; value, acc),
    do: %{acc | v: acc.v + String.to_integer(value)}

  defp d2_destination_reducer("up " &lt;&gt; value, acc),
    do: %{acc | v: acc.v - String.to_integer(value)}

  defp d2_destination_reducer(_, acc), do: acc
</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="233808" data-batch-url="/posts/batch_likers">
                        7
                      </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-2021-day-2/44205/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-233808" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233808"
                     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="233811" data-post-id="233811">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeah, I went for that as well which was really nice but ended up duplicating the move functions for step 2 to change the logic.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"> def process2(input) do
    input
    |&gt; parse_input()
    |&gt; Enum.reduce(%{h: 0, d: 0, aim: 0}, fn val, acc -&gt; move2(acc, val) end)
    |&gt; calc_position
  end

  def parse_input(input) do
    input
    |&gt; String.split("\n", trim: true)
  end

  def calc_position(%{:h =&gt; h, :d =&gt; d}), do: h * d

  def move2(%{:h =&gt; h, :d =&gt; d, :aim =&gt; aim} = p, "forward " &lt;&gt; steps) do
    steps = String.to_integer(steps)
    %{p | :h =&gt; h + steps, :d =&gt; d + aim * steps}
  end

  def move2(%{:aim =&gt; aim} = p, "up " &lt;&gt; steps) do
    steps = String.to_integer(steps)
    %{p | :aim =&gt; aim - steps}
  end

  def move2(%{:aim =&gt; aim} = p, "down " &lt;&gt; steps) do
    steps = String.to_integer(steps)
    %{p | aim: aim + steps}
  end
</code></pre>
<p>And thought putting the accumulator in the first argument would make a nice API but made using it in <code>Enum.reduce</code> more cumbersome.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="233811" 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-2021-day-2/44205/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-233811" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233811"
                     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="233831" data-post-id="233831">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Simple <code>Enum.reduce</code></p>
<p><a href="https://github.com/egze/aoc/blob/master/lib/aoc/y2021/d2.ex" rel="noopener nofollow ugc">y2021/d2.ex</a></p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a href="https://github.com/stefanluptak/advent-of-code/blob/main/lib/day_2.ex" rel="noopener nofollow ugc">Here</a>’s mine.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2 do
  defmodule Part1 do
    def run(input) do
      {depth, distance} =
        Enum.reduce(input, {_depth = 0, _distance = 0}, fn
          {:forward, amount}, {depth, distance} -&gt;
            {depth, distance + amount}

          {:up, amount}, {depth, distance} -&gt;
            {depth - amount, distance}

          {:down, amount}, {depth, distance} -&gt;
            {depth + amount, distance}
        end)

      depth * distance
    end
  end

  defmodule Part2 do
    def run(input) do
      {depth, distance, _aim} =
        Enum.reduce(input, {_depth = 0, _distance = 0, _aim = 0}, fn
          {:forward, amount}, {depth, distance, aim} -&gt;
            depth = depth + aim * amount

            {depth, distance + amount, aim}

          {:up, amount}, {depth, distance, aim} -&gt;
            {depth, distance, aim - amount}

          {:down, amount}, {depth, distance, aim} -&gt;
            {depth, distance, aim + amount}
        end)

      depth * distance
    end
  end

  def input do
    "inputs/2.txt"
    |&gt; File.stream!()
    |&gt; Stream.map(fn line -&gt;
      {instruction_atom, amount_string} =
        case line do
          "up " &lt;&gt; amount -&gt; {:up, amount}
          "down " &lt;&gt; amount -&gt; {:down, amount}
          "forward " &lt;&gt; amount -&gt; {:forward, amount}
        end

      {
        instruction_atom,
        amount_string
        |&gt; String.trim()
        |&gt; String.to_integer()
      }
    end)
  end

  def example_input do
    [
      {:forward, 5},
      {:down, 5},
      {:forward, 8},
      {:up, 3},
      {:down, 8},
      {:forward, 2}
    ]
  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="233832" 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-2021-day-2/44205/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-233832" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233832"
                     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="233841" data-post-id="233841">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my take on day2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day02 do
  def parse(path) do
    {:ok, cont}=File.read(path)
    cont |&gt; String.split(["\n","\r"], trim: true) |&gt; trim()
  end

  def trim([],c), do: c
  def trim([h|t],c) do
    [str,i]=String.split(h," ")
    trim(t,c++[[str,String.to_integer(i)]])
  end
  def trim(l), do: trim(l,[])

  def reg([],n,m), do: n*m
  def reg([["forward",i]|t],n,m), do: reg(t,i+n,m)
  def reg([["down",i]|t],n,m), do: reg(t,n,i+m)
  def reg([["up",i]|t],n,m), do: reg(t,n,m-i)

  def reg2([],n,m,_), do: n*m
  def reg2([["forward",i]|t],n,m,a), do: reg2(t,n+i,m+(a*i),a)
  def reg2([["down",i]|t],n,m,a), do: reg2(t,n,m,i+a)
  def reg2([["up",i]|t],n,m,a), do: reg2(t,n,m,a-i)

  def part1(input), do: reg(parse(input),0,0)

  def part2(input), do: reg2(parse(input),0,0,0)
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="233841" 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-2021-day-2/44205/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-233841" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233841"
                     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="233844" data-post-id="233844">
  <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
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi, <a class="mention" href="/u/hadar_sm" rel="nofollow">@hadar_sm</a>, a small suggestion, avoid using <code>++</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def trim([],c), do: c
  def trim([h|t],c) do
    [str,i]=String.split(h," ")
    trim(t,c++[[str,String.to_integer(i)]])  # &lt;-- here
  end
  def trim(l), do: trim(l,[])
</code></pre>
<p>can be optimized as</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def trim([],c), do: Enum.reverse(c)  # &lt;-- do a reverse when everything is done
  def trim([h|t],c) do
    [str,i]=String.split(h," ")
    trim(t, [[str,String.to_integer(i)] | c])  # &lt;-- prepending instead of appending
  end
  def trim(l), do: trim(l,[])
</code></pre>
<p>This is a very common trick.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here are my solutions. I don’t like Part 1 and i know that it’s not DRY.</p>
<h3><a name="p-233847-part-1-1" class="anchor" href="#p-233847-part-1-1" aria-label="Heading link" rel="nofollow"></a>Part 1</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">instructions =
  File.stream!("data/day_2.txt")
  |&gt; Stream.map(&amp;String.trim/1)
  |&gt; Stream.map(fn str -&gt; String.split(str, " ") end)
  |&gt; Enum.group_by(fn x -&gt; hd(x) end, fn x -&gt; tl(x) end)

down =
  instructions
  |&gt; Map.get("down")
  |&gt; List.flatten()
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.sum()

up =
  instructions 
  |&gt; Map.get("up") 
  |&gt; List.flatten() 
  |&gt; Enum.map(&amp;String.to_integer/1) 
  |&gt; Enum.sum()

forward =
  instructions
  |&gt; Map.get("forward")
  |&gt; List.flatten()
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.sum()

position = (down - up) * forward
</code></pre>
<h3><a name="p-233847-part-2-2" class="anchor" href="#p-233847-part-2-2" aria-label="Heading link" rel="nofollow"></a>Part 2</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">instructions =
  File.stream!("data/day_2.txt")
  |&gt; Stream.map(&amp;String.trim/1)
  |&gt; Stream.map(fn str -&gt; String.split(str, " ") end)
  |&gt; Stream.map(fn [cmd, x] -&gt; [cmd, String.to_integer(x)] end)
  |&gt; Enum.reduce({0, 0, 0}, fn instruction, {depth, aim, horizontal_pos} -&gt;
    case instruction do
      ["forward", x] -&gt;
        {aim * x + depth, aim, horizontal_pos + x}

      ["up", x] -&gt;
        {depth, aim - x, horizontal_pos}

      ["down", x] -&gt;
        {depth, aim + x, horizontal_pos}
    end
  end)
  |&gt; then(fn {depth, _aim, hor_pos} -&gt; depth * hor_pos 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="233847" 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-2021-day-2/44205/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-233847" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233847"
                     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="233852" data-post-id="233852">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I made it with some internal states.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2 do
  defmodule State do
    defstruct [x: 0, depth: 0]

    def apply_command({:forward, x}, %__MODULE__{} = state),
      do: %{state | x: state.x + x}
    def apply_command({:up, depth}, %__MODULE__{} = state),
      do: %{state | depth: state.depth - depth}
    def apply_command({:down, depth}, %__MODULE__{} = state),
      do: %{state | depth: state.depth + depth}
  end

  defmodule State2 do
    defstruct [x: 0, depth: 0, aim: 0]

    def apply_command({:forward, x}, %__MODULE__{} = state),
      do: %{state | x: state.x + x, depth: state.depth + state.aim * x}
    def apply_command({:up, aim}, %__MODULE__{} = state),
      do: %{state | aim: state.aim - aim}
    def apply_command({:down, aim}, %__MODULE__{} = state),
      do: %{state | aim: state.aim + aim}
  end

  def part1 do
    state = load_commands()
    |&gt; Enum.reduce(%State{}, &amp;State.apply_command/2)
    |&gt; IO.inspect(label: "FINAL STATE")

    state.x * state.depth
  end

  def part2 do
    state = load_commands()
    |&gt; Enum.reduce(%State2{}, &amp;State2.apply_command/2)
    |&gt; IO.inspect(label: "FINAL STATE")

    state.x * state.depth
  end

  defp load_commands do
    "input.txt"
    |&gt; File.read!()
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp; to_command(&amp;1))
  end

  defp to_command(binary) when is_binary(binary),
    do: to_command(String.split(binary))

  defp to_command([key, value]) when key in ~w(forward up down),
    do: {String.to_atom(key), String.to_integer(value)}
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="233852" 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-2021-day-2/44205/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-233852" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233852"
                     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="233853" data-post-id="233853">
  <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">
								<p>Here’s mine:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Submarine do
  def input_stream(file) do
    file
    |&gt; File.stream!()
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;String.split/1)
    |&gt; Stream.map(fn [direction, count] -&gt; [direction, String.to_integer(count)] end)
  end

  # Rules for Part1
  def update_position(["forward", count], {horizontal, depth}), do: {horizontal+count, depth}
  def update_position(["up", count], {horizontal, depth}), do: {horizontal, depth-count}
  def update_position(["down", count], {horizontal, depth}), do: {horizontal, depth+count}

  # Rules for Part2
  def update_position(["forward", count], {horizontal, depth, aim}), do: {horizontal+count, depth+count*aim, aim}
  def update_position(["up", count], {horizontal, depth, aim}), do: {horizontal, depth, aim-count}
  def update_position(["down", count], {horizontal, depth, aim}), do: {horizontal, depth, aim+count}
end

[{0, 0}, {0, 0, 0}]
|&gt; Enum.map(fn start_pos -&gt;
  System.argv()
  |&gt; hd()
  |&gt; Submarine.input_stream()
  |&gt; Enum.reduce(start_pos, &amp;Submarine.update_position/2)
end)
|&gt; Enum.with_index(1)
|&gt; Enum.map(fn {tuple, part_number} -&gt;
  result = elem(tuple, 0) * elem(tuple, 1)
  "Part #{part_number}: #{result}"
end)
|&gt; Enum.map(&amp;IO.puts/1)

</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="233853" 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-2021-day-2/44205/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-233853" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233853"
                     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="233856" data-post-id="233856">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution, simple but honest:</p>
<p>Part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Submarine do

  def calc_position(list) do
    forward_sum = sum_movement(list, "forward")
    up_sum = sum_movement(list, "up")
    down_sum = sum_movement(list, "down")  
    depth = down_sum - up_sum
    forward_sum * depth
  end

  defp sum_movement(movs, type) do
    movs
      |&gt; Enum.filter(fn [k,_v] -&gt; k == type end)
      |&gt; Enum.map(fn [_k,v] -&gt; String.to_integer(v) end)
      |&gt; Enum.reduce(fn x, acc -&gt; x + acc end)
  end
end

input
|&gt; String.split(" ")
|&gt; Enum.chunk_every(2)
|&gt; Submarine.calc_position
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Submarine2 do

  def calc_position(list) do
    _calc(list, 0, 0, 0)
  end

  defp _calc([head | tail], aim, horizontal, depth) do
    [key, value] = head
    cond do
      key == "forward" -&gt;
        new_horizontal = horizontal + String.to_integer(value)
        _calc(tail, aim, new_horizontal, calc_depth(depth, aim, String.to_integer(value)) )
      key == "down" -&gt;
        _calc(tail, aim + String.to_integer(value), horizontal, depth)
      key == "up" -&gt;
        _calc(tail, aim - String.to_integer(value), horizontal, depth)
    end
  end

  defp _calc([], _, horizontal, depth) do    
    horizontal * depth
  end

  defp calc_depth(depth, aim, forward_value) do  
    depth + (aim * forward_value)
  end
end

input
|&gt; String.split(" ")
|&gt; Enum.chunk_every(2)
|&gt; Submarine2.calc_position
</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="233856" 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-2021-day-2/44205/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-233856" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="233856"
                     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/44205/load_more?page=3">Load more posts (10 remaining)</a>
</div></template></turbo-stream>