<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="309636" data-post-id="309636">
  <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>I see your code using regexes and it is more concise for this kind of parsing. When there are well defined separators I automatically go for <code>String.split</code> which is more verbose.</p>
<p>Anyway this was easier than day one <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"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Y23.Day2 do
  alias AoC.Input, warn: false

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

  def parse_input(input, _) do
    Enum.map(input, &amp;parse_game/1)
  end

  defp parse_game("Game " &lt;&gt; game) do
    {id, ": " &lt;&gt; rest} = Integer.parse(game)
    hands = rest |&gt; String.split("; ") |&gt; Enum.map(&amp;parse_hand/1)
    {id, hands}
  end

  defp parse_hand(txt) do
    txt
    |&gt; String.split(", ")
    |&gt; Enum.map(&amp;Integer.parse/1)
    |&gt; Enum.reduce({0, 0, 0}, fn
      {n, " red"}, {r, g, b} -&gt; {r + n, g, b}
      {n, " green"}, {r, g, b} -&gt; {r, g + n, b}
      {n, " blue"}, {r, g, b} -&gt; {r, g, b + n}
    end)
  end

  def part_one(problem) do
    problem
    |&gt; Enum.filter(fn {_id, hands} -&gt; Enum.all?(hands, &amp;lte?(&amp;1, {12, 13, 14})) end)
    |&gt; Enum.reduce(0, fn {id, _}, acc -&gt; acc + id end)
  end

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

  defp lte?({r, g, b}, {max_r, max_g, max_b}) do
    r &lt;= max_r and g &lt;= max_g and b &lt;= max_b
  end

  defp power({_id, hands}) do
    {r, g, b} =
      Enum.reduce(hands, fn {r, g, b}, {min_r, min_g, min_b} -&gt;
        {max(min_r, r), max(min_g, g), max(min_b, b)}
      end)

    r * g * b
  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="309636" 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/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-309636" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309636"
                     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="309641" data-post-id="309641">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I spent most of the time on the parser, which I implemented using <a href="https://hexdocs.pm/nimble_parsec/NimbleParsec.html" rel="noopener nofollow ugc">NimbleParsec</a>. I’ve used NimbleParsec before, but it has never became second nature for me, so I still have to refer to the documentation a lot.</p>
<p>This time I learned the hard way that nesting a <code>repeat</code> inside a <code>repeat</code> leads to an infinite loop, such as in this code from my parser:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  cubes = repeat(cube)
  |&gt; optional(ignore(string("; ")))
  |&gt; wrap

  subsets = repeat(cubes)
  |&gt; wrap
</code></pre>
<p>The problem is that the inner <code>repeat</code> will always succeed (by repeating zero times), and therefore the outer <code>repeat</code> will repeat the inner <code>repeat</code> forever.</p>
<p>I solved it by replacing the inner <code>repeat</code> with <code>times</code> with a minimum of one:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  cubes = times(cube, min: 1)
  |&gt; optional(ignore(string("; ")))
  |&gt; wrap

  subsets = repeat(cubes)
  |&gt; wrap
</code></pre>
<hr>
<h1><a name="p-309641-solution-1" class="anchor" href="#p-309641-solution-1" aria-label="Heading link" rel="nofollow"></a>Solution</h1>
<p><a href="https://github.com/bjorng/advent-of-code-2023/blob/main/day02/lib/day02.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/bjorng/advent-of-code-2023/blob/main/day02/lib/day02.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="309641" 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/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-309641" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309641"
                     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="309643" data-post-id="309643">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="christhekeele" data-post="1" data-topic="60090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/christhekeele/48/1039_2.png" class="avatar"> christhekeele:</div>
<blockquote>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def parse_pull_color(result) do
    case Integer.parse(result) do
      {num, " red"} -&gt; {:red, num}
      {num, " green"} -&gt; {:green, num}
      {num, " blue"} -&gt; {:blue, num}
    end
  end
</code></pre>
</blockquote>
</aside>
<p>Just wanted to say I love the use of <code>Integer.parse/1</code> here!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309643" 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/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-309643" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309643"
                     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="309652" data-post-id="309652">
  <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>Again, nothing notable in my code. I wish I took this opportunity to learn Nimble Parsec though, but Elixir syntax is always a joy to parse.</p>
<p><a href="https://github.com/code-shoily/advent_of_code/blob/master/lib/2023/day_02.ex" rel="noopener nofollow ugc">advent_of_code/lib/2023/day_02.ex at master · code-shoily/advent_of_code (github.com)</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="309652" 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/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-309652" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309652"
                     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="309666" data-post-id="309666">
  <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>I like readable code.</p>
<p><a href="https://github.com/nallwhy/advent-of-code/blob/main/2023/day_02.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/nallwhy/advent-of-code/blob/main/2023/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="309666" 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/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-309666" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309666"
                     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="309668" data-post-id="309668">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>In case any of y’all are interested, we are having a ticket giveaway for the 2024 Carolina Code Conference for participants in AoC.</p>
<p>22 tickets with a lot of different ways to win.</p>
<p>Details here:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://blog.carolina.codes/p/advent-of-carolina-code-ticket-challenge">
  <header class="source">
      <img src="https://substackcdn.com/image/fetch/$s_!lqGU!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F117ae989-e6e3-45d0-855e-afb7e2608f33%2Ffavicon.ico" class="site-icon" alt="" width="64" height="64">

      <a href="https://blog.carolina.codes/p/advent-of-carolina-code-ticket-challenge" target="_blank" rel="noopener nofollow ugc">blog.carolina.codes</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/431;"><img src="https://substackcdn.com/image/fetch/$s_!rt2F!,w_1200,h_675,c_fill,f_jpg,q_auto:good,fl_progressive:steep,g_auto/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F01b45f41-60f2-4274-94b0-24dc4c43e867_1080x1080.png" class="thumbnail" alt="" width="690" height="431"></div>

<h3><a href="https://blog.carolina.codes/p/advent-of-carolina-code-ticket-challenge" target="_blank" rel="noopener nofollow ugc">Advent of Carolina Code Ticket Challenge!</a></h3>

  <p>Complete coding challenges to win tickets to the 2024 conference!</p>


  </article>

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

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

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="309668" 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/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-309668" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309668"
                     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="309673" data-post-id="309673">
  <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 Day02 do

  def part2(input) do
    input
    |&gt; String.split("\n")
    |&gt; Enum.map(&amp;String.split(&amp;1, ":"))
    |&gt; Enum.map(&amp;List.last/1)
    |&gt; Enum.map(&amp;power_of_set/1)
    |&gt; Enum.sum
  end

  def part1(input) do
    input
    |&gt; String.split("\n")
    |&gt; Enum.filter(&amp;is_game_possible?/1)
    |&gt; Enum.map(&amp;String.trim_leading(&amp;1,"Game "))
    |&gt; Enum.map(&amp;String.split(&amp;1, ":"))
    |&gt; Enum.map(&amp;List.first/1)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; Enum.sum
  end

  def is_game_possible?(line) do
    line #Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
    |&gt; String.split(":")
    |&gt; List.last
    |&gt; String.split(";")
    |&gt; Enum.map(&amp;to_color_map/1) 
    |&gt; Enum.map(&amp;is_set_possible?/1)
    |&gt; Enum.all?
  end

  def to_color_map(cube_set) do
    cube_set # 3 blue, 4 red
    |&gt; String.split(",")
    |&gt; Enum.reduce( %{red: 0, green: 0, blue: 0}, 
        fn str, acc -&gt;
   
          amount = str 
          |&gt; String.split
          |&gt; List.first
          |&gt; String.to_integer
    
          color = str
          |&gt; String.split
          |&gt; List.last
          |&gt; String.to_atom    
  
          acc
          |&gt; Map.put(color, amount) 

        end)
  end

  def is_set_possible?(map_of_cube_set, 
          limit \\  %{red: 12, green: 13, blue: 14}  ) do
    [ map_of_cube_set.red &lt;= limit.red,
      map_of_cube_set.green &lt;= limit.green,
      map_of_cube_set.blue &lt;= limit.blue   ]
    |&gt; Enum.all? # true if all elements are truthy
  end

  def power_of_set(game) do
    game # 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
    |&gt; String.split(";")
    |&gt; Enum.map(&amp;to_color_map/1)
    |&gt; Enum.reduce(fn color_map, acc -&gt; 
         %{red:   max(color_map.red, acc.red),
           green: max(color_map.green, acc.green),
           blue:  max(color_map.blue, acc.blue) }
          end )
    |&gt; Map.values
    |&gt; Enum.reduce(&amp;Kernel.*/2) # multiplies 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="309673" 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/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-309673" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309673"
                     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="309683" data-post-id="309683">
  <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>Not many <code>defstruct</code>-based solutions yet, so here’s one:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day2Part1 do
  defmodule Game do
    defstruct [:number, :shown, :max_shown]

    def parse(n, shown_str) do
      %Game{
        number: String.to_integer(n),
        shown: parse_shown(String.split(shown_str, ~r{;\s+}))
      }
    end

    defp parse_shown([]), do: []
    defp parse_shown([s|rest]) do
      [parse_one(s) | parse_shown(rest)]
    end

    defp parse_one(s) do
      matches = Regex.scan(~r{(\d+)\s+(\w+)}, s, capture: :all_but_first)

      Map.new(matches, fn [cs, color] -&gt;
        {color, String.to_integer(cs)}
      end)
    end

    def max_shown(game) do
      Enum.reduce(game.shown, %{}, fn el, acc -&gt;
        Map.merge(acc, el, fn _, v1, v2 -&gt; max(v1, v2) end)
      end)
    end

    def fill_max_shown(game) do
      %{game | max_shown: max_shown(game)}
    end

    def valid?(game, target) do
      all_keys = Map.keys(game.max_shown) ++ Map.keys(target)

      Enum.all?(all_keys, fn k -&gt; game.max_shown[k] &lt;= target[k] end)
    end
  end

  def read(filename) do
    File.stream!(filename)
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;Regex.run(~r{^Game (\d+):\s+(.*)$}, &amp;1, capture: :all_but_first))
    |&gt; Stream.map(fn [n, shown] -&gt; Game.parse(n, shown) end)
  end
end

target_cubes = %{"red" =&gt; 12, "green" =&gt; 13, "blue" =&gt; 14}

Day2Part1.read("input.txt")
|&gt; Stream.map(&amp;Day2Part1.Game.fill_max_shown/1)
|&gt; Stream.filter(&amp;Day2Part1.Game.valid?(&amp;1, target_cubes))
|&gt; Stream.map(&amp; &amp;1.number)
|&gt; Enum.sum()
|&gt; IO.inspect()
</code></pre>
<p>Some thoughts:</p>
<ul>
<li><code>parse_shown</code> is recursive for no particular reason; it could equally well be written as another <code>Enum.map</code></li>
<li><code>fill_max_shown</code> feels a little strange tacked on at the end, but I was pre-optimizing for a part 2 that did something totally different (which was NOT what happened in part 2 <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"> )</li>
<li>interesting factoid: the only place that the color names appear in the code is in <code>target_cubes</code>, the computations are independent of the specific keys</li>
</ul>
<p>For part 2, only one more function on <code>Game</code> is needed:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    def power(game) do
      game.max_shown
      |&gt; Map.values()
      |&gt; Enum.reduce(1, &amp;Kernel.*/2)
    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="309683" 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/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-309683" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309683"
                     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="309684" data-post-id="309684">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My essential “genius” part of code was this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def max_takes(takes) do
    Enum.reduce(takes, %{}, fn take, acc -&gt;
      Enum.reduce(take, acc, fn {k, v}, acc -&gt;
        Map.update(acc, k, v, fn prev_v -&gt;
          if v &gt; prev_v do
            v
          else
            prev_v
          end
        end)
      end)
    end)
  end
</code></pre>
<p>I already see ideas from other solutions on how it can be simplified.<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"><br>
Full solution:<br>
<a href="https://gitlab.com/mrsk/aoc-elixir/-/blob/main/lib/Aoc2023/D02.ex" rel="noopener nofollow ugc">https://gitlab.com/mrsk/aoc-elixir/-/blob/main/lib/Aoc2023/D02.ex</a></p>
<p>Btw. Is that a little cute trebuchet in the bottom left corner of calendar?</p>
<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="309684" 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/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-309684" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309684"
                     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>
    <div class="postbit" id="309689" data-post-id="309689">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That was a pleasant one</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Day02 do
  defmodule Parser do
    import NimbleParsec

    game_id =
      string("Game ")
      |&gt; ignore()
      |&gt; integer(min: 1, max: 3)
      |&gt; ignore(string(": "))

    cube_set =
      times(
        integer(min: 1, max: 3)
        |&gt; ignore(ascii_char([?\s]))
        |&gt; choice([string("red"), string("green"), string("blue")])
        |&gt; ignore(optional(ascii_char([?,])))
        |&gt; ignore(optional(ascii_char([?\s]))),
        min: 1,
        max: 3
      )
      |&gt; reduce(:cube_set_reducer)
      |&gt; ignore(optional(string("; ")))

    cube_sets = repeat(cube_set)

    game =
      game_id
      |&gt; wrap(cube_sets)
      |&gt; ignore(optional(ascii_char([?\n])))
      |&gt; wrap()

    games = repeat(game)

    defparsec(:game_id, game_id)
    defparsec(:cube_set, cube_set)
    defparsec(:cube_sets, cube_sets)
    defparsec(:game, game)
    defparsec(:games, games)

    def cube_set_reducer(x) do
      for [k, v] &lt;- Enum.chunk_every(x, 2), do: {v, k}, into: %{}
    end
  end

  def parse!(input, parsec \\ :games) do
    {:ok, result, "", %{}, _, _} = apply(Parser, parsec, [input])
    result
  end

  def part1(input, bag \\ %{}) do
    input
    |&gt; parse!()
    |&gt; Enum.filter(&amp;game_possible?(&amp;1, bag))
    |&gt; Enum.map(fn [game_id, _] -&gt; game_id end)
    |&gt; Enum.sum()
  end

  def part2(input) do
    input
    |&gt; parse!()
    |&gt; Enum.map(&amp;minimal_bag/1)
    |&gt; Enum.map(&amp;power/1)
    |&gt; Enum.sum()
  end

  def power(minimal_bag) do
    minimal_bag
    |&gt; Map.values()
    |&gt; Enum.reduce(1, &amp;(&amp;1 * &amp;2))
  end

  def minimal_bag([_game_id, cube_sets]) do
    Enum.reduce(cube_sets, %{"red" =&gt; 0, "green" =&gt; 0, "blue" =&gt; 0}, fn cube_set, acc -&gt;
      Enum.map(acc, fn {k, v} -&gt;
        case cube_set[k] do
          new when is_integer(new) and new &gt; v -&gt; {k, new}
          _ -&gt; {k, v}
        end
      end)
      |&gt; Enum.into(%{})
    end)
  end

  def game_possible?([_game_id, cube_sets], bag) do
    any_exceeding? =
      Enum.find(cube_sets, fn cube_set -&gt;
        Enum.any?(cube_set, fn {k, v} -&gt; is_nil(bag[k]) or bag[k] &lt; v end)
      end)

    !any_exceeding?
  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="309689" 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/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-309689" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="309689"
                     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>
</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/60090/load_more?page=3">Load more posts (10 remaining)</a>
</div></template></turbo-stream>