sasajuric

sasajuric

Author of Elixir In Action

Advent of Code - Day 5

Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.


This is my Day 5. My other days are a bit of a mess (constantly refactoring to try out different ways to make it look better). With day 5 though, I saw a lot of places for a bit of optimization like storing the length in the fully_react reduction.

First Post!

balbin

balbin

Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.

For general discussion about the Advent of Code 2018 and links to topics of the other days, see this topic.


This is my Day 5. My other days are a bit of a mess (constantly refactoring to try out different ways to make it look better). With day 5 though, I saw a lot of places for a bit of optimization like storing the length in the fully_react reduction.

Most Liked

Gazler

Gazler

Phoenix Core Team

Day 5 fits really nicely with binary pattern matching:

chrismcg

chrismcg

My original solution was a pretty hacky regexy thing in rust. After I’d finished and looked at the solutions mega thread the Haskell foldr five liner at the top was a bit of a blow :smile:. I made an elixir version which uses lists rather than binaries which I think is nice too:

(EDIT: very similar to Ryan’s solution above too)

defmodule Day5 do
  @moduledoc """
  Day 5 Elixir solutions for Advent of Code 2018
  """

  @doc """
  Removes all reacting units from the input and returns the final length

  Algorithm from a Haskell implementation in reddit:
  https://www.reddit.com/r/adventofcode/comments/a3912m/2018_day_5_solutions/eb4dchg/

  ## Examples

      iex> Day5.react("dabAcCaCBAcCcaDA")
      10

  """
  def react(input) do
    input
    |> String.to_charlist()
    |> List.foldr([], &react/2)
    |> Enum.count()
  end

  # two units react when the difference in their ASCII code is 32
  defguard reacts(unit, prev_unit) when abs(unit - prev_unit) == 32

  # handle the start case
  defp react(unit, []), do: [unit]
  # handle when the last two elements in the list react
  defp react(unit, [prev_unit]) when reacts(unit, prev_unit), do: []
  # if there's a reaction just return the tail to remove reacting elements
  defp react(unit, [prev_unit | tail]) when reacts(unit, prev_unit), do: tail
  # all good, just add the unit to the list
  defp react(unit, tail), do: [unit | tail]
end

simon

simon

I’ve been astonished at the speed of some of José’s solutions. I don’t think any of mine are ridiculously slow but comparatively they are.

Solving the problems is one element of the challenge. Making the code faster is another. Making it faster and still understandable is where I struggle the most as an Elixir newbie.

My plan is to re-do the challenges in January to see how my code has evolved over the challenge which is not a long period of time but I’m writing a lot of code and learning new things every day.

Where Next?

Popular in Challenges Top

bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
bjorng
Note: This topic is to talk about Day 18 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
ehayun
I have 2 arrays: a1 can be any combination of value or nil like that a1 = [1,nil,3] and array 2 the same a2 = [4,2, nil] How do I com...
New
bjorng
This topic is about Day 1 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adven...
New
bjorng
Here is my solution for day 2 of Advent of Code: https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
code-shoily
Here’s my day 3 code https://github.com/code-shoily/advent_of_code/blob/master/lib/2024/day_03.ex This was quite easy. I was afraid Par...
New
lud
Hello everyone! This year is going to be shorter, but the difficulty will grow faster. Today I already feel that this is not standard “D...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement