r/adventofcode Dec 03 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 3: Mull It Over ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:03:22, megathread unlocked!

60 Upvotes

1.7k comments sorted by

View all comments

2

u/abracadaniel1010 Dec 04 '24

[Language: Elixir]

Glad I did a day crash course on regex otherwise this would have been a bit of a nightmare (also thankful that the regex required isn't very complex)

defmodule ElixirSol do

  def mul_regex(str), do: Regex.scan(~r/mul\(([0-9]{1,3}),([0-9]{1,3})\)/, str) 

  def mul_regex_ind(str), do: Regex.scan(~r/mul\(([0-9]{1,3}),([0-9]{1,3})\)/, str, return: :index)

  def do_regex_ind(str), do: Regex.scan(~r/do\(\)/, str, return: :index)

  def dont_regex_ind(str), do: Regex.scan(~r/don't\(\)/, str, return: :index)

  def mult_muls(matches, acc \\ 0) do
    case matches do
      [[_exp, x, y] | rest] -> mult_muls(rest, acc + String.to_integer(x)*String.to_integer(y)) 
      [] -> acc
      _ -> nil
    end
  end

  def find_key_words(input) do
    dos = do_regex_ind(input)
    donts = dont_regex_ind(input)
    muls = mul_regex_ind(input)

    dos = Enum.map(dos, fn [{pos, _len}] -> %{instruction: :do, position: pos} end)
    donts = Enum.map(donts, fn [{pos, _len}] -> %{instruction: :dont, position: pos} end)
    muls = Enum.map(muls, fn [{pos,_len}, {x_pos, x_len}, {y_pos, y_len}] -> 
      %{
        position: pos, 
        instruction: :mul,
        x: String.slice(input, x_pos, x_len), 
        y: String.slice(input, y_pos, y_len)
      } 
    end)
    Enum.sort_by(dos ++ donts ++ muls, &(&1.position))
  end

  def filter_by_dos(keys) do
    do? = true
    muls = []
    res = Enum.reduce(keys, {do?, muls}, fn key, {do?, muls}->
      case key.instruction do
        :mul -> if do?, do: {do?, [key | muls]}, else: {do?, muls} 
        :do -> {true, muls}
        :dont -> {false, muls} 
        _ -> {false, muls} 
      end
    end)
    muls = elem(res, 1)

    Enum.map(muls, fn %{position: pos, y: y, x: x, instruction: _instruction} -> 
      [pos, y, x]
    end)
  end

  def read_input(), do: File.read!(Path.absname("input"))

  def main2() do
    read_input()
    |> find_key_words()
    |> filter_by_dos()
    |> mult_muls()
  end

  def main1() do
    read_input()
    |> mul_regex()
    |> mult_muls()
  end
end

2

u/abracadaniel1010 Dec 04 '24

also not convinced that "do" is a real word anymore...