<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="309615" data-post-id="309615">
  <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>I used a regex trick that I just learned yesterday.</p>
<p>My code is messy so won’t share it. But I’m gonna share my trick.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Regex.scan(~r/(?=(\d+))/, "123456789", capture: :all_but_first)
</code></pre>
<p>returns</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  ["123456789"],
  ["23456789"],
  ["3456789"],
  ["456789"],
  ["56789"],
  ["6789"],
  ["789"],
  ["89"],
  ["9"]
]
</code></pre>
<p>The key is the <code>(?=)</code> part and the <code>()</code> combined with the <code>capture: :all_but_first</code> option, not the <code>\d+</code> part in the regex.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I stand corrected (I was in fact thinking of Ruby)! Updated my comment and referenced yours, thanks for teaching me something today.</p> 
	            </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kip" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/120/1440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kip
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>ex_cldr Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve not done AoC before so this was a fun diversion for a Saturday morning.</p>
<p>For part 1 I had a simple <code>for</code> comprehension that I think is quite easy to understand:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def extract_integer(text) do
    [first, last] =
      for &lt;&lt;char &lt;- text&gt;&gt;, char in ?0..?9, reduce: [nil, nil] do
        [nil, nil] -&gt; [char - ?0, char - ?0]
        [first, _] -&gt; [first, char - ?0]
      end

    first * 10 + last
  end
</code></pre>
<p>But it runs in about 500 μs on my MacBook Pro (M1 Max) and uses about 1.6 MB of memory whereas my final solution just uses recursive functions and it finishes in less than 200 μs using less than 128 KB of memory.</p>
<details><summary>Part 1</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def extract_integer_1b(&lt;&lt;&gt;&gt;, [first, last]) do
    first * 10 + last
  end

  def extract_integer_1b(&lt;&lt;char, rest::binary&gt;&gt;, [nil, nil]) when char in ?0..?9 do
    extract_integer_1b(rest, [char - ?0, char - ?0])
  end

  def extract_integer_1b(&lt;&lt;char, rest::binary&gt;&gt;, [first, _]) when char in ?0..?9 do
    extract_integer_1b(rest, [first, char - ?0])
  end

  def extract_integer_1b(&lt;&lt;_char, rest::binary&gt;&gt;, acc) do
    extract_integer_1b(rest, acc)
  end
</code></pre>
</details>
<p>The second part just builds on the first part. And, like others, it generates the function heads for the recursive functions from static data. This runs in about 300 μs.</p>
<details><summary>Part 2</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @integer_map %{
    "zero" =&gt; 0,
    "one" =&gt; 1,
    "two" =&gt; 2,
    "three" =&gt; 3,
    "four" =&gt; 4,
    "five" =&gt; 5,
    "six" =&gt; 6,
    "seven" =&gt; 7,
    "eight" =&gt; 8,
    "nine" =&gt; 9
  }

  def extract_integer_2(&lt;&lt;&gt;&gt;, [first, last]) do
    first * 10 + last
  end

  for {text, int} &lt;- @integer_map do
    last_char = String.last(text)

    def extract_integer_2(&lt;&lt;unquote(text) , rest::binary&gt;&gt;, [nil, nil]) do
      extract_integer_2(&lt;&lt;unquote(last_char), rest::binary&gt;&gt;, [unquote(int), unquote(int)])
    end

    def extract_integer_2(&lt;&lt;unquote(text) , rest::binary&gt;&gt;, [first, _]) do
      extract_integer_2(&lt;&lt;unquote(last_char), rest::binary&gt;&gt;, [first, unquote(int)])
    end
  end

  def extract_integer_2(&lt;&lt;char, rest::binary&gt;&gt;, [nil, nil]) when char in ?0..?9 do
    extract_integer_2(rest, [char - ?0, char - ?0])
  end

  def extract_integer_2(&lt;&lt;char, rest::binary&gt;&gt;, [first, _]) when char in ?0..?9 do
    extract_integer_2(rest, [first, char - ?0])
  end

  def extract_integer_2(&lt;&lt;_char, rest::binary&gt;&gt;, acc) do
    extract_integer_2(rest, acc)
  end
</code></pre>
</details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309617" 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-2023-day-1/60073/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-309617" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309617"
                     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="309619" data-post-id="309619">
  <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>Wow some solutions here are cool.</p>
<p>I used a simple regular expressions based approach</p>
<p><a href="https://github.com/code-shoily/advent_of_code/blob/master/lib/2023/day_01.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/code-shoily/advent_of_code/blob/master/lib/2023/day_01.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="309619" 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-2023-day-1/60073/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-309619" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309619"
                     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="309632" data-post-id="309632">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution does have to parse all numbers instead of just the first and last, and does not support “oNe” <a class="mention" href="/u/al2o3cr" rel="nofollow">@al2o3cr</a> <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"> but it’s still fast.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Y23.Day1 do
  alias AoC.Input, warn: false

  def read_file(file, _part) do
    Input.stream!(file, trim: true)
  end

  def parse_input(input, _part) do
    input
  end

  def part_one(problem) do
    problem
    |&gt; Enum.map(&amp;String.to_charlist/1)
    |&gt; Enum.map(&amp;solve_line_p1/1)
    |&gt; Enum.reduce(&amp;Kernel.+/2)
  end

  defp solve_line_p1(chars) do
    digits = Enum.filter(chars, &amp;(&amp;1 in ?0..?9))
    {first, last} = first_last(digits)
    List.to_integer([first, last])
  end

  defp first_last(items) do
    [h | _] = items
    [g | _] = :lists.reverse(items)
    {h, g}
  end

  def part_two(problem) do
    problem
    |&gt; Enum.map(&amp;solve_line_p2/1)
    |&gt; Enum.reduce(&amp;Kernel.+/2)
  end

  defp solve_line_p2(line) do
    line
    |&gt; collect_digits([])
    |&gt; first_last()
    |&gt; then(fn {a, b} -&gt; a * 10 + b end)
  end

  defp collect_digits(&lt;&lt;c, rest::binary&gt;&gt;, acc) when c in ?0..?9,
    do: collect_digits(rest, [c - ?0 | acc])

  ~w(one two three four five six seven eight nine)
  |&gt; Enum.with_index(1)
  |&gt; Enum.each(fn {&lt;&lt;c, keep::binary&gt;&gt;, digit} -&gt;
    defp collect_digits(&lt;&lt;unquote(c), unquote(keep)::binary, rest::binary&gt;&gt;, acc) do
      collect_digits(&lt;&lt;unquote(keep)::binary, rest::binary&gt;&gt;, [unquote(digit) | acc])
    end
  end)

  defp collect_digits(&lt;&lt;_, rest::binary&gt;&gt;, acc), do: collect_digits(rest, acc)
  defp collect_digits(&lt;&lt;&gt;&gt;, acc), do: :lists.reverse(acc)
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="309632" 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-2023-day-1/60073/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-309632" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309632"
                     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="309659" data-post-id="309659">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can also use a capture in an positive lookahead to write the whole thing as a one-liner using Perl:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">perl -pe 's/^[^\d]*(?=(\d)).*(\d)[^\d]*$/\1\2\n/' input.txt | paste -s -d+ - | bc
</code></pre>
<p>The lookahead is critical because otherwise inputs like <code>abc4def</code> won’t find the <code>4</code> twice.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309659" 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-2023-day-1/60073/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-309659" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309659"
                     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="309665" data-post-id="309665">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here the my solution that uses macro to generate functions</p>
<p><a href="https://github.com/nallwhy/advent-of-code/blob/main/2023/day_01.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/nallwhy/advent-of-code/blob/main/2023/day_01.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="309665" 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-2023-day-1/60073/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-309665" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309665"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #27"></div>
  </section>
</div>
    <div class="postbit" id="309672" data-post-id="309672">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My beginner’s solution</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day01 do

  def part2(str) do
    str
    |&gt; String.split
    |&gt; Enum.map(&amp;spelled_out_words_to_digits/1)
    |&gt; Enum.map(&amp;recover_two_digit_number/1)
    |&gt; Enum.sum  
  end
  
  def part1(str) do
    str
    |&gt; String.split
    |&gt; Enum.map(&amp;recover_two_digit_number/1)
    |&gt; Enum.sum  
  end

  def recover_two_digit_number(str) do
    digits = str
      |&gt; String.graphemes
      |&gt; Enum.filter(&amp;is_digit?/1) 

    List.first(digits) &lt;&gt; List.last(digits)
    |&gt; String.to_integer
    
  end

  def is_digit?(s) do
    s |&gt; String.contains?(["0", "1", "2", "3", 
                  "4", "5", "6", "7", "8", "9"])
  end 

  def spelled_out_words_to_digits(str) do
    str
    |&gt; String.replace("one", "one1one")
    |&gt; String.replace("two", "two2two")
    |&gt; String.replace("three", "three3three")
    |&gt; String.replace("four", "four4four")
    |&gt; String.replace("five", "five5five")
    |&gt; String.replace("six", "six6six")
    |&gt; String.replace("seven", "seven7seven")
    |&gt; String.replace("eight", "eight8eight")
    |&gt; String.replace("nine", "nine9nine")
    
  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="309672" 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-2023-day-1/60073/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-309672" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309672"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #28"></div>
  </section>
</div>
    <div class="postbit" id="309681" data-post-id="309681">
  <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>I am using Livebook for my solution.</p>
<details>
<summary>
Part 1</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">Kino.FS.file_path("01.txt")
|&gt; File.stream!()
|&gt; Stream.map(fn line -&gt;
  matches =
    Regex.scan(~r/\d/, line)
    |&gt; List.flatten()

  first = List.first(matches)
  last = List.last(matches)

  String.to_integer("#{first}#{last}")
end)
|&gt; Enum.sum()
</code></pre>
</details>
<details>
<summary>
Part 2</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">pattern = ~r/(?=(\d|one|two|three|four|five|six|seven|eight|nine))/

Kino.FS.file_path("01.txt")
|&gt; File.stream!()
|&gt; Stream.map(fn line -&gt;
  matches =
    Regex.scan(pattern, line, capture: :all_but_first)
    |&gt; List.flatten()
    |&gt; Enum.map(fn
      "one" -&gt; "1"
      "two" -&gt; "2"
      "three" -&gt; "3"
      "four" -&gt; "4"
      "five" -&gt; "5"
      "six" -&gt; "6"
      "seven" -&gt; "7"
      "eight" -&gt; "8"
      "nine" -&gt; "9"
      input -&gt; input
    end)

  first = List.first(matches)
  last = List.last(matches)

  String.to_integer(first &lt;&gt; last)
end)
|&gt; Enum.sum()
</code></pre>
</details>
<p>You can also find the code <a href="https://github.com/pehbehbeh/adventofcode/blob/main/2023/01.livemd" rel="noopener nofollow ugc">on GitHub</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="309681" 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-2023-day-1/60073/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-309681" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309681"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #29"></div>
  </section>
</div>
    <div class="postbit" id="309686" data-post-id="309686">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Really was not prepared for the overlapping text integers. In fact, based on my example input I assumed that was explicitly NOT supposed to be valid. Does not bode well for my performance long term this year, LOL.</p>
<p>My focus was on using a single pass over the string, i.e., avoiding one pass forward and another pass over the reversed string. Wish I had known about the <code>:global</code> flag for <code>String.replace</code> as I think that would have been cleaner than the regex solution I’m using.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
    @digit_strings %{
      "one" =&gt; "1",
      "two" =&gt; "2",
      "three" =&gt; "3",
      "four" =&gt; "4",
      "five" =&gt; "5",
      "six" =&gt; "6",
      "seven" =&gt; "7",
      "eight" =&gt; "8",
      "nine" =&gt; "9"
    }

    @regex ("(?=(" &lt;&gt; (@digit_strings |&gt; Map.keys() |&gt; Enum.join("|") |&gt; Kernel.&lt;&gt;("))")))
           |&gt; Regex.compile()
           |&gt; elem(1)


    def parse(input) do
      input
      |&gt; Day1.input()
      |&gt; Input.lines()
      |&gt; Enum.map(fn line -&gt;
        Regex.replace(@regex, line, fn _, x -&gt; Map.get(@digit_strings, x) end)
      end)
  end

def do_solve(data) do
      data
      |&gt; Enum.map(fn line -&gt;
        first_and_last_ints(line)
        |&gt; String.to_integer()
      end)
      |&gt; Enum.sum()
    end

    defp first_and_last_ints(line) when is_binary(line) do
      line
      |&gt; String.graphemes()
      |&gt; Enum.reduce({"", nil}, fn g, {s, last} -&gt;
        with {i, ""} &lt;- Integer.parse(g) do
          if s == "" do
            {s &lt;&gt; "#{i}", i}
          else
            {s, i}
          end
        else
          _ -&gt;
            {s, last}
        end
      end)
      |&gt; append_last()
    end

    defp append_last({s, i}), do: s &lt;&gt; "#{i}"
</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="309686" 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-2023-day-1/60073/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-309686" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309686"
                     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 #30"></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/60073/load_more?page=4">Load more posts (16 remaining)</a>
</div></template></turbo-stream>