Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2023

2023/02.livemd

Advent of Code 2023

Mix.install([
  {:kino, "~> 0.14.0"},
  {:kino_aoc, "~> 0.1"},
  {:kino_vega_lite, "~> 0.1.11"}
])

Introduction

Proceed with caution! I’m learning Elixir here, this is not good Elixir code. I skipped Day 1, just warming up for 2024.

Day 2

{:ok, day2} = KinoAOC.download_puzzle("2023", "2", System.fetch_env!("LB_AOC_SESSION"))
defmodule Day2 do
  def parse(str) do
    str |> String.split("\n", trim: true) |> Enum.map(&parse_game/1)
  end

  def parse_game(str) do
    [g, sets] = String.split(str, ": ", trim: true)

    sets = sets |> String.split("; ", trim: true) |> Enum.map(&String.split(&1, ", ", trim: true))

    sets = sets |> Enum.map(fn s ->
      Enum.map(s, fn x ->
        [count, color] = String.split(x, " ", trim: true)

        { color, String.to_integer(count) }
      end)
    end)
      |> Enum.map(&Map.new/1)
      |> Enum.reduce(%{}, fn map, acc ->
            Map.merge(acc, map, fn _key, val1, val2 -> max(val1, val2) end)
          end)

    [g, sets]
  end
end
Day2.parse(day2)
  |> Enum.filter(fn [_g, colors] -> 
    Map.get(colors, "red", 0) <= 12 &amp;&amp; Map.get(colors, "green", 0) <= 13 &amp;&amp; Map.get(colors, "blue", 0) <= 14
  end)
  |> Enum.map(fn [g, _colors] ->
    String.replace_leading(g, "Game ", "") 
    |> String.to_integer end)
  |> Enum.sum

Part 2

Day2.parse(day2)
|> Enum.map(fn [g, colors] ->
  [g, Map.get(colors, "red", 1) * Map.get(colors, "green", 1) * Map.get(colors, "blue", 1)]
  end)
|> Enum.map(fn [_g, pow] -> pow end) |> Enum.sum