bjorng

bjorng

Erlang Core Team

Note: This topic is to talk about Day 4 of the Advent of Code 2019.

There is a private leaderboard for elixirforum members. You can join it by following this link and entering the following code:

39276-eeb74f9a

Showing Posts 1 to 10

bjorng

bjorng OP

Erlang Core Team

Here is my solution.

Aetherus

Aetherus

Very unhygienic code, especially for part 2.

Part 1

359999..799999
|> Stream.map(&Integer.to_string/1)
|> Stream.map(&String.graphemes/1)
|> Stream.reject(fn graphemes -> graphemes |> Stream.chunk_every(2, 1, :discard) |> Enum.any?(fn [a, b] -> a > b end) end)
|> Stream.reject(fn graphemes -> graphemes |> Stream.chunk_every(2, 1, :discard) |> Enum.all?(fn [a, b] -> a != b end) end)
|> Enum.count()
|> IO.inspect()

Part 2

359999..799999
|> Stream.map(&Integer.to_string/1)
|> Stream.map(&String.graphemes/1)
|> Stream.reject(fn graphemes -> graphemes |> Stream.chunk_every(2, 1, :discard) |> Enum.any?(fn [a, b] -> a > b end) end)
|> Stream.map(fn graphemes -> [:x] ++ graphemes ++ [:x] end)  # tricky padding
|> Stream.filter(fn graphemes -> graphemes |> Stream.chunk_every(4, 1, :discard) |> Enum.any?(fn
     [a, b, b, c] when a != b and b != c -> true
     _ -> false
   end) end)
|> Enum.count()
|> IO.inspect()
aaronnamba

aaronnamba

For AoC, I pick a language I want to get to know better. This year it’s Elixir. I came from the Ruby world (by way of a short detour through Crystal-land). I have already launched a Phoenix-based CMS (closed source for now, unfortunately), but I am still far from fluent.

So I’m sure my solutions won’t be ideal, but I thought I’d post them anyway, since i have enjoyed reading through the various solutions posted here for the first 3 days.

Day 4 Solution

As I was writing this, Aetherus’s post appeared and I realized I forgot about the :discard option on chunk_every… oh well.

Aetherus

Aetherus

Glad to have helped :slightly_smiling_face:

qhwa

qhwa

Here is my solution. I feel it can still be improved.

Edit:
I simplified it after reading @yuchunc 's nice solution. This is always the most enjoyable part during the AOC sessions, because I can learn so much from others here. Thank you guys!

albertarv

albertarv

I used quite a few pattern matchiing clauses for the strict double checking
https://github.com/scmx/advent-of-code-2019-elixir/blob/master/lib/day_04_secure_container.ex

aaronnamba

aaronnamba

One of the first languages I ever learned was Perl (now called Raku?!?), so I tend to reach for regexes a lot, perhaps more than I should. Maybe regexes are a no-no in Elixir world, I don’t know.

This regex needed backreferences, so I was afraid it might turn out super slow, but in this case, the strings involved are so short that it didn’t matter.

  @doc """
      iex> Day4.PasswordFinder2.remove_long_repeats("112233")
      "112233"
      iex> Day4.PasswordFinder2.remove_long_repeats("123444")
      "123"
      iex> Day4.PasswordFinder2.remove_long_repeats("111122")
      "22"
  """
  def remove_long_repeats(str) do
    String.replace(str, ~r/(\d)\1{2,}/, "")
  end
yuchunc

yuchunc

My Day4 Solution

https://github.com/yuchunc/aoc2019/blob/master/lib/day4/day4.ex

Any feedbacks are welcome! :slight_smile:

Aetherus

Aetherus

How to prevent someone cheating by using other languages? I personally used Ruby in Day 2.

aaronnamba

aaronnamba

Your part 2 check is really nice, I wish I’d thought of that.

One small thing I noticed, things like:

  defp increment?([d1 | [d2 | _]]) when d1 > d2, do: false

can be:

  defp increment?([d1, d2 | _]) when d1 > d2, do: false

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

Agostinho1965
Hey everyone — I’m putting together a practical, code-first book on building production-ready business applications with Phoenix LiveView...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews