sasajuric
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.
Most Liked
Gazler
Day 5 fits really nicely with binary pattern matching:
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
. 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
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.
Popular in Challenges
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








