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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my attempt - new to Elixir for AOC 24</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day2 do

  # opts = [headers: [{"cookie", "session=#{System.fetch_env!("AOC_SESSION_COOKIE")}"}]]
  # input = Req.get!("https://adventofcode.com/2024/day/2/input", opts).body

  def part_1(input) do
    input 
      |&gt; String.split("\n", trim: true)
      |&gt; Enum.map(&amp;convert_to_int_list/1)
      |&gt; Enum.map(&amp;check_report/1)
      |&gt; Enum.count(fn s -&gt; elem(s, 0) == :safe end)     
  end

  def part_2(input) do
    input 
      |&gt; String.split("\n", trim: true)
      |&gt; Enum.map(&amp;convert_to_int_list/1)
      |&gt; Enum.map(&amp;safe_combinations?/1)
      |&gt; Enum.count(fn s -&gt; s == true end)
  end

  defp convert_to_int_list(readings) do
    readings
    |&gt; String.split()
    |&gt; Enum.map(&amp;String.to_integer/1)    
  end

  defp safe_combinations?(reading) do
    [reading | (for idx &lt;- (0..length(reading) - 1), do: List.delete_at(reading, idx))]
    |&gt; Enum.map(&amp;check_report/1)
    |&gt; Enum.any?(fn s -&gt; elem(s, 0) == :safe end)
  end

  defp check_report(reading) do
    reading
    |&gt; Enum.reduce({:unknown}, fn x, acc -&gt; 
      case acc do
        {:unsafe} -&gt; 
          {:unsafe}
        {:unknown} -&gt; 
          {:safe, :unknown, x}
        {:safe, :unknown, prev} when prev &lt; x and abs(prev - x) &lt; 4  -&gt;
          {:safe, :increasing, x}
        {:safe, :unknown, prev} when prev &gt; x and abs(prev - x) &lt; 4 -&gt;
          {:safe, :decreasing, x}
        {:safe, :increasing, prev} when prev &lt; x and abs(prev - x) &lt; 4 -&gt;
          {:safe, :increasing, x}
        {:safe, :decreasing, prev} when prev &gt; x and abs(prev - x) &lt; 4 -&gt;
          {:safe, :decreasing, x}
        _ -&gt;
          {:unsafe}            
      end    
      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="348095" 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-2024-day-2/67804/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-348095" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348095"
                     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="348109" data-post-id="348109">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I am going to learn from your code. I tried something similar but failed (got 15 reports lower), I am guessing I missed one of the <code>{:safe, :unknown}</code> case. Thanks for sharing.</p>
<p>EDIT: Nevermind, looks like I didn’t do it like this, I had used a fold with  <code>idx</code> list to combine for the cases. But this code gave me an idea any way <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348109" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-348109" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348109"
                     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="348111" data-post-id="348111">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="billylanchantin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/billylanchantin/120/28428_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  billylanchantin
                  </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">def part1(file), do: file |&gt; file_to_lists_of_ints |&gt; Enum.count(&amp;safe?/1)

def safe?(line) do
  pairs = Enum.chunk_every(line, 2, 1, :discard)
  [a, [h | t]] = pairs |&gt; Enum.map(fn [x, y] -&gt; [abs(y - x), y &gt; x] end) |&gt; Enum.zip_with(&amp; &amp;1)
  Enum.all?(a, &amp;(0 &lt; &amp;1 and &amp;1 &lt; 4)) and Enum.all?(t, &amp;(&amp;1 == h))
end
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def part2(file) do
  file
  |&gt; file_to_lists_of_ints
  |&gt; Enum.count(fn line -&gt; line |&gt; drop1 |&gt; Enum.any?(&amp;safe?/1) end)
end

def drop1(l), do: for(i &lt;- 0..(length(l) - 1), do: List.delete_at(l, i))
</code></pre>
<p>Boilerplate:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def file_to_lists_of_ints(file) do
  file |&gt; File.read!() |&gt; String.trim() |&gt; String.split("\n") |&gt; Enum.map(&amp;String.to_integer/1)
end
</code></pre>
<p>EDIT: bit o’ code golf (same approach, fewer <code>def</code>s).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348111" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-348111" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348111"
                     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="348113" data-post-id="348113">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Just brute forcing all the possibilities…</p>
<p><a href="https://github.com/pehbehbeh/adventofcode/blob/main/2024/02.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/pehbehbeh/adventofcode/blob/main/2024/02.livemd</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="348113" 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-2024-day-2/67804/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-348113" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348113"
                     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="348121" data-post-id="348121">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>After looking at some other replies, I can use a range to whittle my <code>safe?</code> down more:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def safe?(line) do
  [h | t] = line |&gt; Enum.chunk_every(2, 1, :discard) |&gt; Enum.map(fn [x, y] -&gt; y - x end)
  Enum.all?([h | t], &amp;(abs(&amp;1) in 1..3)) and Enum.all?(t, &amp;(&amp;1 &gt; 0 == h &gt; 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="348121" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-348121" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348121"
                     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="348125" data-post-id="348125">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I too tried to find a way not to brute force it, but in the end caved to brute force:</p>
<p><a href="https://github.com/jswanner/aoc/blob/main/2024/day-02.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jswanner/aoc/blob/main/2024/day-02.livemd</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="348125" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-348125" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348125"
                     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="348134" data-post-id="348134">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>After way too many hours at least i’ve got a working solution to part a.<br>
It’s really hard for me to unlearn what i learned from non functional languages over the years and i see that i overcomplicate things that can be written way more elegant. But hey, i’m learning the language for like three days now and AoC gives me a lot of opportunity for trial and error (And watching other solutions to learn more about the standard library - i need to have a look at the reduce functions!)</p>
<p>Today i learned: <strong>cond</strong> - I was getting incorrect results because i tried to rebuild something “if… else if… else”-alike.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day2 do
  def solve_a do
    reports =
      File.stream!("input")
      |&gt; Enum.map(&amp;String.trim(&amp;1, "\n"))
      |&gt; Enum.map(&amp;String.split/1)

    check_a(reports, 0, 0)
  end

  @doc "Execute the checks recusively for each report"
  def check_a([h | t], s, u) do

    current_report =
      Enum.map(h, fn(x) -&gt; String.to_integer(x) end)

    if safe?(current_report) do
      check_a(t, s+1, u)
    else
      check_a(t, s, u+1)
    end
  end
  def check_a([], safe, unsafe) do
    IO.puts "Safe: #{Integer.to_string(safe)} - Unsafe: #{Integer.to_string(unsafe)}"
  end

  def safe?([h1, h2 | t] = report) do
    if difference_safe?(h1, h2 , t) do
      if continuous?(report, :init) do
        true
      else
        false
      end
    else
      false
    end
  end

  def safe?([_ | []]) do
    true
  end

  def difference_safe?(a, b, [ h | t ]) do
    case abs(a - b) &lt;= 3 do
      true -&gt; difference_safe?(b, h, t)
      false -&gt; false
    end
  end

  def difference_safe?(a, b, []) do
    case abs(a - b) &lt;= 3 do
      true -&gt; true
      false -&gt; false
    end
  end

  def continuous?([a, b | t] = report, direction) do
    cond do
      a &gt; b -&gt;
        case direction do
          :asc -&gt;  continuous?([b | t], :asc)
          :init -&gt;  continuous?([b | t], :asc)
          _ -&gt;  false
        end
      a &lt; b -&gt;
        case direction do
          :desc -&gt; continuous?([b | t], :desc)
          :init -&gt; continuous?([b | t], :desc)
          _ -&gt;  false
        end
      a == b -&gt;
        false
    end
  end

  def continuous?([ _ ], _) do
    true
  end
end
</code></pre>
<p>Let’s see if i can get my head around part 2 today, too <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="348134" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-348134" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348134"
                     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="348140" data-post-id="348140">
  <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>My second day solution took me much longer than necessary.</p>
<p>I managed to get most of it done between getting the kids to the bus and having my first meeting of the day. Though part B was off… And throughout the day, I took some minutes between meetings or during breaks to try to fix this.</p>
<p>Now, 12 hours after I have started the AoC day, I found a working solution, and in hindsight, my previous iterations, either skipped the variant with only the first or the last level, which lead to skewed results <img src="https://forum.elixirforum.com/images/emoji/apple/slightly_frowning_face.png?v=15" title=":slightly_frowning_face:" class="emoji" alt=":slightly_frowning_face:" loading="lazy" width="20" height="20"></p>
<p><a href="https://gitlab.com/NobbZ/aoc_ex/-/blob/main/lib/y2024/d02.ex?ref_type=heads" class="onebox" target="_blank" rel="noopener nofollow">https://gitlab.com/NobbZ/aoc_ex/-/blob/main/lib/y2024/d02.ex?ref_type=heads</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="348140" 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-2024-day-2/67804/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-348140" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348140"
                     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="348149" data-post-id="348149">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve ended up doing something that stops computation asap instead of brute force all possibilities.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule D2 do
  def p1(file) do
    file
    |&gt; reports()
    |&gt; Stream.filter(&amp;safe_report_p1?/1)
    |&gt; Enum.count()
  end

  defp reports(file) do
    file
    |&gt; File.stream!(:line)
    |&gt; Stream.map(fn line -&gt;
      line |&gt; String.split() |&gt; Enum.map(&amp;String.to_integer/1)
    end)
  end

  def safe_report_p1?(report) do
    safe_report_p1?(report, :asc) or safe_report_p1?(report, :desc)
  end

  def safe_report_p1?([], _) do
    true
  end
  def safe_report_p1?([_], _) do
    true
  end
  def safe_report_p1?([a, b | rest], :asc) when a &lt; b and b - a &lt;= 3 do
    safe_report_p1?([b | rest], :asc)
  end
  def safe_report_p1?([a, b | rest], :desc) when a &gt; b and a - b &lt;= 3 do
    safe_report_p1?([b | rest], :desc)
  end
  def safe_report_p1?(_, _) do
    false
  end

  def p2(file) do
    file
    |&gt; reports()
    |&gt; Stream.filter(&amp;safe_report_p2?/1)
    |&gt; Enum.count()
  end

  def safe_report_p2?(report) do
    safe_report_p2?([], report, :asc) or safe_report_p2?([], report, :desc)
  end

  def safe_report_p2?(_, [], _) do
    true
  end
  def safe_report_p2?(_, [_], _) do
    true
  end
  def safe_report_p2?(scanned, [a, b | rest], :asc) when a &lt; b and b - a &lt;= 3 do
    safe_report_p2?(scanned ++ [a], [b | rest], :asc)
  end
  def safe_report_p2?(scanned, [a, b | rest], :desc) when a &gt; b and a - b &lt;= 3 do
    safe_report_p2?(scanned ++ [a], [b | rest], :desc)
  end
  def safe_report_p2?(scanned, [a, b | rest], asc_or_desc) do
    safe_report_p1?(scanned ++ [a | rest], asc_or_desc) or
      safe_report_p1?(scanned ++ [b | rest], asc_or_desc)
  end
  def safe_report_p2?(_, _, _) do
    false
  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="348149" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-348149" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348149"
                     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="348161" data-post-id="348161">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution to today’s story.</p>
<p>Possible optimizations:</p>
<ul>
<li>detect misleading direction by a → b immediately. Although with 4 reports in my input data that would be catched sooner, I doubt it would be more performant. It would however catch invalid input early on, so that’s something worth too.</li>
<li>there’s a very shitty return type in the code. <a class="mention" href="/u/dimitarvp" rel="nofollow">@dimitarvp</a> ..you have been warned <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"></li>
<li>spot the major hole in the logic. I was surprised my data set passed….</li>
</ul>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day02 do
  alias AoC.Input

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

  defp parse_line(line) do
    line
    |&gt; String.split()
    |&gt; Enum.map(&amp;String.to_integer/1)
  end

  def part_one(problem) do
    Enum.count(problem, &amp;(save?(&amp;1) == true))
  end

  defp save?(levels) do
    [a, b | _] = levels
    direction = a &lt; b &amp;&amp; :asc || :desc

    # returns true or index....got fired.
    save?(levels, direction)
  end

  defp save?(_report, _direction, idx \\ 0)

  defp save?([x, y | t], :desc, idx),
    do: (x - y &gt; 0 &amp;&amp; (x - y) in 1..3 &amp;&amp; save?([y|t], :desc, idx + 1)) || idx

  defp save?([x, y | t], :asc, idx),
    do: (x - y &lt; 0 &amp;&amp; (x - y) in -3..-1 &amp;&amp; save?([y|t], :asc, idx + 1)) || idx

  defp save?([_], _, _), do: true

  def part_two(problem) do
    Enum.count(problem, &amp;save_as_statistics?/1)
  end

  defp save_as_statistics?(report) do
    case save?(report) do
      true -&gt;
        true

      idx -&gt;
        # politician mode: enabled
        # 1. the idx is at fault.
        # 2. the next idx was at fault.
        # 3. we started on the wrong foot.
        cond do
          List.delete_at(report, idx) |&gt; save?() == true -&gt; true
          List.delete_at(report, idx + 1) |&gt; save?() == true -&gt; true
          List.delete_at(report, 0) |&gt; save?() == true -&gt; true
          true -&gt; false
          false -&gt; true # it does not even trigger warnings.
        end
    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="348161" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2024-day-2/67804/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-348161" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348161"
                     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/67804/load_more?page=3">Load more posts (7 remaining)</a>
</div></template></turbo-stream>