<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="309690" data-post-id="309690">
  <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>My solution using Livebook.</p>
<p>I avoided Regex this time and did a lot of String splitting.</p>
<p>After solving part 2 I adjusted part 1 to also work with the aggregated “max cubes needed”.</p>
<p><a href="https://github.com/pehbehbeh/adventofcode/blob/main/2023/02.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/pehbehbeh/adventofcode/blob/main/2023/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="309690" 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-2/60090/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-309690" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309690"
                     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="309717" data-post-id="309717">
  <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 overkilled it with <a href="https://www.erlang.org/doc/man/yecc.html" rel="nofollow">yecc</a> <img src="https://forum.elixirforum.com/images/emoji/apple/joy.png?v=15" title=":joy:" class="emoji" alt=":joy:" loading="lazy" width="20" height="20"></p>
<p>Here’s the content my <code>aoc2023_day2_parser.yrl</code> file:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Terminals  game  num  red  green  blue  ':'  ','  ';'.

Nonterminals  color  games  one_game  set  sets.

Rootsymbol  games.

color -&gt; num red : {red, element(3, '$1')}.
color -&gt; num green : {green, element(3, '$1')}.
color -&gt; num blue : {blue, element(3, '$1')}.

set -&gt; color : ['$1'].
set -&gt; color ',' set : ['$1' | '$3'].

sets -&gt; set : ['$1'].
sets -&gt; set ';' sets : ['$1' | '$3'].

one_game -&gt; game num ':' sets : {element(3, '$2'), '$4'}.

games -&gt; one_game : ['$1'].
games -&gt; one_game games : ['$1' | '$2'].
</code></pre>
<p>FYI,</p>
<p>The parser generated by <code>yecc</code> with the <code>.yrl</code> file expects a series of <strong>tokens</strong>, not the text input, so we need another program to transform the text into tokens.</p>
<p>A token is either <code>{category_name(), metadata(), value()}</code> or <code>{category_name(), metadata()}</code> if that category contains only 1 value.</p>
<ul>
<li><code>@type category_name() :: atom()</code></li>
<li><code>@type metadata() :: term()</code></li>
<li><code>@type value() :: term()</code></li>
</ul>
<p>The first line of the <code>.yrl</code> file declares what <strong>terminal category</strong> names will appear in this file. The name of a terminal category is just the <code>category_name()</code> part of a token.</p>
<p>The second line of the <code>.yrl</code> file declares what <strong>non-terminal categories</strong> will be defined in this file. A non-terminal category is a category that is composed of one or more terminal or non-terminal categories. We’ll see this later.</p>
<p>The third line tells the parser generator which category will be the root. The value of the root is the top-level value that’ll be returned by the parser.</p>
<p>The rest lines define the non-terminal categories. The syntax of these lines is</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">NonterminalCategoryName -&gt; Syntax : Reducer.
</code></pre>
<p>The things appear in the <code>Syntax</code> part are all category names (terminal or non-terminal).</p>
<p>A <code>Reducer</code> is just a piece of Erlang code that converts the categories in the <code>Syntax</code> part to a value (any Erlang term you want). The <code>'$n'</code> in a <code>Reducer</code> part refers to the value of the n-th (1-based) category in the <code>Syntax</code> part.</p>
<p>Here is my lexer (a program that converts the text input to a series of tokens):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AoC2023.Day2.Lexer do
  def tokenize(input) do
    do_tokenize(input, [])
  end

  defp do_tokenize("", acc) do
    Enum.reverse([{:"$end", []} | acc])
  end

  defp do_tokenize("Game" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:game, []} | acc])
  end

  defp do_tokenize("red" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:red, []} | acc])
  end

  defp do_tokenize("green" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:green, []} | acc])
  end

  defp do_tokenize("blue" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:blue, []} | acc])
  end

  defp do_tokenize("," &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:",", []} | acc])
  end

  defp do_tokenize(";" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:";", []} | acc])
  end

  defp do_tokenize(":" &lt;&gt; rest, acc) do
    do_tokenize(rest, [{:":", []} | acc])
  end

  defp do_tokenize(&lt;&lt;char, rest::binary&gt;&gt;, [{:num, _, num} | acc])
       when char in ?0..?9 do
    do_tokenize(rest, [{:num, [], num * 10 + char - ?0} | acc])
  end

  defp do_tokenize(&lt;&lt;char, rest::binary&gt;&gt;, acc)
       when char in ?0..?9 do
    do_tokenize(rest, [{:num, [], char - ?0} | acc])
  end

  defp do_tokenize(&lt;&lt;_, rest::binary&gt;&gt;, acc) do
    do_tokenize(rest, acc)
  end
end
</code></pre>
<p>The tokens it produces are like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[
  {:game, []},
  {:num, [], 1},
  {:":", []},
  {:num, [], 1},
  {:blue, []},
  {:";", []},
  {:num, [], 4},
  {:green, []},
  {:",", []},
  {:num, [], 5},
  {:blue, []},
  ...
]
</code></pre>
<p>Whole solution:</p>
<p><a href="https://github.com/Aetherus/advent-of-code/blob/master/2023/day-02-yecc.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/Aetherus/advent-of-code/blob/master/2023/day-02-yecc.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="309717" 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-2/60090/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-309717" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309717"
                     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="309719" data-post-id="309719">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>import AOC<br>
import String, only: [split: 2, to_integer: 1]<br>
import Enum, only: [map: 2, max: 1, product: 1, reduce: 3, sum: 1, zip_with: 2]</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">aoc 2023, 2 do
  def parse_game(line) do
    ["Game " &lt;&gt; id, hands] = split(line, ": ")
    {to_integer(id), hands |&gt; split("; ") |&gt; map(&amp;parse_hand/1)}
  end

  def parse_hand(hand) do
    hand |&gt; split(", ") |&gt; reduce([0,0,0], &amp;parse_count/2)
  end

  def parse_count(count, [red, green, blue]) do
    case split(count, " ") do
      [n, "red"]   -&gt; [to_integer(n), green, blue]
      [n, "green"] -&gt; [red, to_integer(n), blue]
      [n, "blue"]  -&gt; [red, green, to_integer(n)]
    end
  end

  def common(input, f) do
    input |&gt; split("\n") |&gt; map(&amp;(&amp;1 |&gt; parse_game() |&gt; f.())) |&gt; sum()
  end

  def p1(input) do
    common(
      input,
      fn {id, games} -&gt;
        [r, g, b] = zip_with(games, &amp;max/1)
        if(r &lt;= 12 and g &lt;= 13 and b &lt;= 14, do: id, else: 0)
      end)
  end

  def p2(input) do
    common(input, fn {_, hands} -&gt; hands |&gt; zip_with(&amp;max/1) |&gt; product() 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="309719" 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-2/60090/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-309719" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309719"
                     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="309720" data-post-id="309720">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution, was also lucky to just reuse the code for the second task</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Puzzle2 do
  def get_game_id(line) do
    Regex.named_captures(~r/(?:Game )(?&lt;game_id&gt;\d+)/, line)
  end

  def find_number_of_cubes(line) do
    Regex.scan(~r/(?:(?&lt;greens&gt;\d+) green)|(?:(?&lt;blues&gt;\d+) blue)|(?:(?&lt;reds&gt;\d+) red)/, line,
      capture: :all_names
    )
  end

  def get_max_cube_count(counts) do
    counts
    |&gt; Enum.filter(fn x -&gt; x != "" end)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; Enum.concat([0])
    |&gt; Enum.max()
  end

  def find_max_per_color(cubes) do
    %{
      :blue =&gt;
        Enum.map(cubes, fn [blue, _, _] -&gt; blue end)
        |&gt; get_max_cube_count(),
      :green =&gt;
        Enum.map(cubes, fn [_, green, _] -&gt; green end)
        |&gt; get_max_cube_count(),
      :red =&gt;
        Enum.map(cubes, fn [_, _, red] -&gt; red end)
        |&gt; get_max_cube_count()
    }
  end

  def build_game_info(line) do
    find_number_of_cubes(line)
    |&gt; find_max_per_color()
    |&gt; Map.merge(get_game_id(line))
  end

  def game_is_possible?(game, avail) do
    game.blue &lt;= avail.blue and game.red &lt;= avail.red and game.green &lt;= avail.green
  end
end

cubes = %{:green =&gt; 13, :red =&gt; 12, :blue =&gt; 14}
# Task1
puzzle_data
|&gt; String.split("\n", trim: true)
|&gt; Enum.map(&amp;Puzzle2.build_game_info/1)
|&gt; Enum.filter(&amp;Puzzle2.game_is_possible?(&amp;1, cubes))
|&gt; Enum.reduce(0, fn %{"game_id" =&gt; gid}, acc -&gt; acc + String.to_integer(gid) end)
|&gt; dbg()

# Task2
puzzle_data
|&gt; String.split("\n", trim: true)
|&gt; Enum.map(&amp;Puzzle2.build_game_info/1)
|&gt; Enum.reduce(0, fn %{:green =&gt; green, :red =&gt; red, :blue =&gt; blue}, acc -&gt;
  acc + green * red * blue
end)
|&gt; dbg()
</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="309720" 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-2/60090/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-309720" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309720"
                     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="309721" data-post-id="309721">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is my take on Day 2. My focus was on parsing the text input and processing the lists of maps. <code>Enum.all?/2</code> and <code>Map.merge/3</code> helped keep things under control (mainly).</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2 do
  @test_bag %{"red" =&gt; 12, "green" =&gt; 13, "blue" =&gt; 14}

  def part_1(path \\ "day2/sample.txt") do
    FileHelper.read_file(path)
    |&gt; parse_input()
    |&gt; sum_possible_games()
  end

  def part_2(path \\ "day2/sample.txt") do
    FileHelper.read_file(path)
    |&gt; parse_input()
    |&gt; sum_minimum_cubes()
  end

  defp parse_input(input) do
    input
    |&gt; Enum.map(fn row -&gt;
      String.split(row, ": ")
    end)
    |&gt; Enum.map(fn [raw_id, raw_sets] -&gt;
      id = String.split(raw_id, " ") |&gt; List.last() |&gt; String.to_integer()

      sets =
        raw_sets
        |&gt; String.split("; ")
        |&gt; Enum.map(fn game -&gt;
          String.split(game, ", ")
        end)
        |&gt; Enum.map(fn game -&gt;
          game
          |&gt; Enum.map(fn x -&gt; String.split(x, " ") end)
          |&gt; Enum.map(&amp;List.to_tuple/1)
          |&gt; Map.new(fn {val, key} -&gt; {key, String.to_integer(val)} end)
        end)

      {id, sets}
    end)
  end

  defp sum_possible_games(input) do
    input
    |&gt; Enum.filter(&amp;possible_game?/1)
    |&gt; Enum.map(fn {id, _} -&gt; id end)
    |&gt; Enum.sum()
  end

  defp possible_game?({id, sets}) do
    case Enum.all?(sets, fn set -&gt;
           Enum.all?(set, fn {color, count} -&gt;
             Map.has_key?(@test_bag, color) &amp;&amp; @test_bag[color] &gt;= count
           end)
         end) do
      false -&gt; false
      true -&gt; id
    end
  end

  defp sum_minimum_cubes(input) do
    input
    |&gt; Enum.map(&amp;calculate_smallest_cube_count_for_game/1)
    |&gt; Enum.map(&amp;Enum.product/1)
    |&gt; Enum.sum()
  end

  defp calculate_smallest_cube_count_for_game({_id, sets}) do
    sets
    |&gt; Enum.reduce(%{"red" =&gt; 0, "green" =&gt; 0, "blue" =&gt; 0}, fn set, acc -&gt;
      Map.merge(acc, set, fn _k, v1, v2 -&gt; Enum.max([v1, v2]) end)
    end)
    |&gt; Map.values()
  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="309721" 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-2/60090/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-309721" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309721"
                     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="309732" data-post-id="309732">
  <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>My code <a href="https://github.com/stevensonmt/advent_of_code/blob/2023/2023/day2/lib/day2.ex" rel="noopener nofollow ugc">here</a>. The only notable thing was I decided to force myself to parse manually doing nothing but pattern matching on binaries. Painful and pointless, but also was kind of a fun challenge. Only worked because I knew the limits of the input size. It would fail if any round included any cubes of more than 99 or if there were more than 100 games. Since I knew neither of those conditions applied it was fine.</p>
<p>Here’s the parsing:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def parse(input) do
      input
      |&gt; Day2.input()
      |&gt; Input.lines()
      |&gt; Enum.map(&amp;parse_game/1)
      |&gt; Enum.map(fn map -&gt;
        [k] = Map.keys(map)

        v =
          Map.values(map)
          |&gt; hd()
          |&gt; un_nest()

        {k, v}
      end)
      |&gt; Enum.into(%{})
    end

    defp parse_game(&lt;&lt;"Game ", rest::binary&gt;&gt;), do: parse_game(rest)

    defp parse_game(&lt;&lt;i, j, ": ", rest::binary&gt;&gt;) when i in 49..57 and j in 48..57 do
      k = (i - 48) * 10 + (j - 48)
      Map.put(%{}, k, parse_game(rest))
    end

    defp parse_game(&lt;&lt;i, ": ", rest::binary&gt;&gt;) when i in 49..57,
      do: %{(i - 48) =&gt; parse_game(rest)}

    defp parse_game(&lt;&lt;"100: ", rest::binary&gt;&gt;), do: %{100 =&gt; parse_game(rest)}

    defp parse_game(&lt;&lt;i, " blue", rest::binary&gt;&gt;) when i in 49..57 do
      [{"blue", i - 48} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;i, " red", rest::binary&gt;&gt;) when i in 49..57 do
      [{"red", i - 48} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;i, " green", rest::binary&gt;&gt;) when i in 49..57 do
      [{"green", i - 48} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;i, j, " blue", rest::binary&gt;&gt;) when i in 49..57 and j in 48..57 do
      [{"blue", (i - 48) * 10 + (j - 48)} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;i, j, " red", rest::binary&gt;&gt;) when i in 49..57 and j in 48..57 do
      [{"red", (i - 48) * 10 + (j - 48)} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;i, j, " green", rest::binary&gt;&gt;) when i in 49..57 and j in 48..57 do
      [{"green", (i - 48) * 10 + (j - 48)} | parse_game(rest)]
    end

    defp parse_game(&lt;&lt;", ", rest::binary&gt;&gt;), do: parse_game(rest)
    defp parse_game(&lt;&lt;"; ", rest::binary&gt;&gt;), do: [parse_game(rest)]
    defp parse_game(""), do: []

    defp un_nest(nested, m \\ %{})
    defp un_nest([], m), do: [m]

    defp un_nest([hd], m) when is_list(hd),
      do: un_nest(hd) ++ [m]

    defp un_nest([{k, v} | tl], m), do: un_nest(tl, Map.put(m, k, v))
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir"></code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309732" data-batch-url="/posts/batch_likers">
                        4
                      </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-2/60090/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-309732" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309732"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="309806" data-post-id="309806">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>A bit late. Trying to learn to use Elixir for this years advent of code. Coming from mostly Ruby. Here was my solution.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def part1(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.filter(&amp;match?(%{green: g, red: r, blue: b} when g &lt;= 13 and r &lt;= 12 and b &lt;= 14, bag_max(&amp;1)))
    |&gt; Enum.map(fn s -&gt; Regex.run(~r/Game (\d+)/, s) |&gt; List.last |&gt; String.to_integer end)
    |&gt; Enum.sum()
  end

  def part2(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;bag_max/1)
    |&gt; Enum.map(fn c -&gt; c[:green] * c[:red] * c[:blue] end)
    |&gt; Enum.sum()
  end

  def bag_max(line) do
    Regex.scan(~r/(\d+) (blue|red|green)/,line)
    |&gt; Enum.map(&amp;tl/1)
    |&gt; Enum.group_by(&amp;String.to_atom(List.last(&amp;1)), &amp;String.to_integer(List.first(&amp;1)))
    |&gt; Enum.into(%{}, fn {k,v} -&gt; {k, Enum.max(v)} 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="309806" 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-2/60090/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-309806" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309806"
                     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="309845" data-post-id="309845">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve used Regex.named_captures too in order to parse each game.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp parse_game(game) do
    ["Game " &lt;&gt; id | sets] = String.split(game, [":", ";"])

    parsed_sets =
      sets
      |&gt; Enum.map(fn set -&gt;
        ["red", "blue", "green"]
        |&gt; Enum.map(fn color -&gt;
          case Regex.named_captures(~r/(?&lt;#{color}&gt;[0-9]*) #{color}/, set) do
            nil -&gt; 0
            %{^color =&gt; count} -&gt; String.to_integer(count)
          end
        end)
        |&gt; List.to_tuple()
      end)

    {String.to_integer(id), parsed_sets}
  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="309845" 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-2/60090/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-309845" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309845"
                     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="310139" data-post-id="310139">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Most of the time on this was spent parsing with <code>String.split/3</code>.  Regex probably would have been faster.  The rest of the logic was trivial</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp game_possible?({_game_number, sets}) do
  Enum.all?(sets, &amp;Enum.all?(&amp;1, fn {color, count} -&gt; count &lt;= @limits[color] end))
end

defp minimum_necessary_set({_game_number, sets}) do
  Enum.reduce(sets, %{}, &amp;Map.merge(&amp;1, &amp;2, fn _key, v1, v2 -&gt; max(v1, v2) end))
end
</code></pre>
<p><a href="https://github.com/APB9785/AoC-2023-elixir/blob/master/lib/day_02.ex" rel="noopener nofollow ugc">https://github.com/APB9785/AoC-2023-elixir/blob/master/lib/day_02.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="310139" 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-2/60090/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-310139" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310139"
                     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>
    <div class="postbit" id="310938" data-post-id="310938">
  <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>Similar to <a class="mention" href="/u/bjorng" rel="nofollow">@bjorng</a>, I wanted to try out NimbleParsec for fun. Looks like we had fairly different parser definitions, but the “post processing” bits seemed similar.<br>
I also included an example of how I may do a similar approach in regex, which I saw others do something similar</p>
<p><a href="https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2023/d02.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2023/d02.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="310938" 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-2/60090/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-310938" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310938"
                     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>