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

First Post!

bjorng

bjorng OP

Erlang Core Team

Here is my solution.

Most Liked

bossek

bossek

defmodule Day04 do
  import Enum, only: [filter: 2, chunk_by: 2, any?: 2, sort: 1]
  def run(cnd), do: 124_075..580_769 |> filter(&valid?(Integer.digits(&1), cnd)) |> length()
  defp valid?(ds, cnd), do: chunk_by(ds, & &1) |> any?(&cnd.(length(&1))) and sort(ds) == ds
end

IO.inspect(Day04.run(&(&1 >= 2)), label: "part 1")
IO.inspect(Day04.run(&(&1 == 2)), label: "part 2")
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.

sasajuric

sasajuric

Author of Elixir In Action

My solution is here.
I decided I don’t want to brute-force this, so I made a function which computes the next valid password, on top of which I can build a stream of valid passwords.

Last Post!

Tuxified

Tuxified

I went for a quick and dirty approach, using just a for comprehension and some Enum functions:

defmodule PasswordDigger do
  @moduledoc """
  Documentation for PasswordDigger.
  A function to calc all possible combinations
  given a set of rules:
  - each digit is equal or higher than previous
  - six digits
  - at least one pair (two same digits)
  - range: 109165-576723
  """

  def part1 do
    items =
      for a <- 1..5,
          b <- a..9,
          c <- b..9,
          d <- c..9,
          e <- d..9,
          f <- e..9,
          Integer.undigits([a, b, c, d, e, f]) < 576_723,
          Enum.uniq([a, b, c, d, e, f]) != [a, b, c, d, e, f],
          do: Integer.undigits([a, b, c, d, e, f])

    IO.puts("Number of possible passwords is: #{Enum.count(items)}")
  end

  def part2 do
    large_group_filter = fn password ->
      password
      |> Enum.group_by(& &1)
      |> Enum.any?(fn {_k, vs} -> Enum.count(vs) == 2 end)
    end

    items =
      for a <- 1..5,
          b <- a..9,
          c <- b..9,
          d <- c..9,
          e <- d..9,
          f <- e..9,
          Integer.undigits([a, b, c, d, e, f]) < 576_723,
          Enum.uniq([a, b, c, d, e, f]) != [a, b, c, d, e, f],
          large_group_filter.([a, b, c, d, e, f]),
          do: Integer.undigits([a, b, c, d, e, f])

    IO.puts("Number of possible passwords is: #{Enum.count(items)}")
  end
end

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews