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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If I understand what you’re getting at: I think substituting <code>Enum.map</code> with <code>Stream.map</code>, in this case, effectively does what you’re talking about. Can’t say if it’ll perform better/worse, but the input list will only be passed over once</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270198" 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-2022-day-2/52183/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-270198" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270198"
                     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="270202" data-post-id="270202">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Like most others, went with the direct approach once i figured there were only 9 cases. Although today I remembered to use File.stream</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/tfwright/bfab5e3105c027e4a52f52ae06031e2a">
  <header class="source">

      <a href="https://gist.github.com/tfwright/bfab5e3105c027e4a52f52ae06031e2a" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/tfwright/bfab5e3105c027e4a52f52ae06031e2a" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/tfwright/bfab5e3105c027e4a52f52ae06031e2a</a></h4>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>Although with all the magic numbers here I would probably use constants for the score calculations even in a first iteration.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270202" 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-2022-day-2/52183/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-270202" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270202"
                     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="270336" data-post-id="270336">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I prefer the solution from <a class="mention" href="/u/tfwright" rel="nofollow">@tfwright</a>. It is faster and cleaner. Should have used <code>Enum.reduce/3</code>.</p>
<p>part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyModule do
  def outcome_score(65, 88), do: 3 # Rock     A - X Rock
  def outcome_score(65, 89), do: 6 # Rock     A - Y Paper
  def outcome_score(65, 90), do: 0 # Rock     A - Z Scissors
  def outcome_score(66, 88), do: 0 # Paper    B - X Rock
  def outcome_score(66, 89), do: 3 # Paper    B - Y Paper
  def outcome_score(66, 90), do: 6 # Paper    B - Z Scissors
  def outcome_score(67, 88), do: 6 # Scissors C - X Rock
  def outcome_score(67, 89), do: 0 # Scissors C - Y Paper
  def outcome_score(67, 90), do: 3 # Scissors C - Z Scissors

  def shape_score(88), do: 1 # X
  def shape_score(89), do: 2 # Y
  def shape_score(90), do: 3 # Z
end

start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|&gt; Stream.map(fn &lt;&lt;opponent, " ", me, "\n"&gt;&gt; -&gt; MyModule.shape_score(me) + MyModule.outcome_score(opponent, me) end)
|&gt; Enum.sum()
|&gt; tap(fn sum -&gt; IO.puts "Total score: #{sum}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"
</code></pre>
<p>part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyModule do
  def score(65, 88), do: 0+3 # Rock     A - X Lose =&gt; Scissors
  def score(65, 89), do: 3+1 # Rock     A - Y Draw =&gt; Rock
  def score(65, 90), do: 6+2 # Rock     A - Z Win  =&gt; Paper
  def score(66, 88), do: 0+1 # Paper    B - X Lose =&gt; Rock
  def score(66, 89), do: 3+2 # Paper    B - Y Draw =&gt; Paper
  def score(66, 90), do: 6+3 # Paper    B - Z Win  =&gt; Scissors
  def score(67, 88), do: 0+2 # Scissors C - X Lose =&gt; Paper
  def score(67, 89), do: 3+3 # Scissors C - Y Draw =&gt; Scissors
  def score(67, 90), do: 6+1 # Scissors C - Z Win  =&gt; Rock
end

start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|&gt; Stream.map(fn &lt;&lt;opponent, " ", goal, "\n"&gt;&gt; -&gt; MyModule.score(opponent, goal) end)
|&gt; Enum.sum()
|&gt; tap(fn sum -&gt; IO.puts "Total score: #{sum}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µ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="270336" 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-2022-day-2/52183/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-270336" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270336"
                     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="270420" data-post-id="270420">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My take.<br>
part 1 and part 2 computed at the same time in the reduce. I wanted to avoid hard-coding, and keep it below 20 lines of code.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">File.stream!("input")
|&gt; Enum.map(&amp; String.to_charlist(&amp;1) |&gt; List.to_tuple() )
|&gt; Enum.reduce({0, 0}, fn {i, _, j, _}, {acc_part1, acc_part2} -&gt;
  { acc_part1 + j - 87 +
    case Integer.mod(j - i - (?X-?A), 3) do
      2 -&gt; 0       # loose
      x -&gt; (x+1)*3 # win or draw
    end,
    acc_part2 + case j do
      ?X -&gt; Integer.mod(i - ?A - 1, 3) + 1 + 0 # loose
      ?Y -&gt; Integer.mod(i - ?A + 0, 3) + 1 + 3 # draw
      ?Z -&gt; Integer.mod(i - ?A + 1, 3) + 1 + 6 # win
    end
  }
end)
|&gt; IO.inspect()

</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="270420" 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-2022-day-2/52183/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-270420" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270420"
                     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="270429" data-post-id="270429">
  <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>Part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">input
|&gt; String.split("\n", trim: true)
|&gt; Enum.reduce(0, fn
  "A X", acc -&gt; acc + 3 + 1
  "A Y", acc -&gt; acc + 6 + 2
  "A Z", acc -&gt; acc + 0 + 3

  "B X", acc -&gt; acc + 0 + 1
  "B Y", acc -&gt; acc + 3 + 2
  "B Z", acc -&gt; acc + 6 + 3

  "C X", acc -&gt; acc + 6 + 1
  "C Y", acc -&gt; acc + 0 + 2
  "C Z", acc -&gt; acc + 3 + 3
end)
</code></pre>
<p>Part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">input
|&gt; String.split("\n", trim: true)
|&gt; Enum.reduce(0, fn
  "A X", acc -&gt; acc + 0 + 3
  "A Y", acc -&gt; acc + 3 + 1
  "A Z", acc -&gt; acc + 6 + 2

  "B X", acc -&gt; acc + 0 + 1
  "B Y", acc -&gt; acc + 3 + 2
  "B Z", acc -&gt; acc + 6 + 3

  "C X", acc -&gt; acc + 0 + 2
  "C Y", acc -&gt; acc + 3 + 3
  "C Z", acc -&gt; acc + 6 + 1
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="270429" 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-2022-day-2/52183/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-270429" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270429"
                     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="270674" data-post-id="270674">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can find my solution here : <a href="https://livebook.dev/run?url=https://gitlab.com/jfburdet/adventofcode/-/raw/main/2022/day2.livemd" class="inline-onebox" rel="noopener nofollow ugc">Run - Livebook.dev</a>  (comments are in french, but code is pretty expressive)</p>
<p>Here are some interesting part : the function playing the game :</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2Helpers.Outcome do
  # Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock.
  # If both players choose the same shape, the round instead ends in a draw.

  def round_outcome({:scissors, :rock}), do: :win
  def round_outcome({:paper, :scissors}), do: :win
  def round_outcome({:rock, :paper}), do: :win

  def round_outcome({opponent, me}) when opponent == me do
    :draw
  end

  def round_outcome({_, _}), do: :loose
end
</code></pre>
<p>Then for part two, I tried playing the game until I find the expected result :</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @shapes [:rock, :paper, :scissors]
#[...]
  defp c2_shape("X", opponent) do
    # Let's play the game and keep only loosing input shapes.
    @shapes
    |&gt; Enum.filter(fn shape -&gt; Day2Helpers.Outcome.round_outcome({opponent, shape}) == :loose end)
    |&gt; Enum.at(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="270674" 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-2022-day-2/52183/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-270674" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270674"
                     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="270759" data-post-id="270759">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m pretty new to Elixir, so feedback is always appreciated!</p>
<p><a href="https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day2.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/dmarcoux/advent_of_code_2022/blob/main/lib/advent_of_code2022/day2.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="270759" 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-2022-day-2/52183/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-270759" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270759"
                     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>