bjorng

bjorng

Erlang Core Team

Advent of Code 2019 - Day 4

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

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.

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()
yuchunc

yuchunc

My Day4 Solution

Any feedbacks are welcome! :slight_smile:

Where Next?

Popular in Challenges Top

shritesh
I mapped both the cards and every possible hand to numeric values and sorted them. In part 2 I could only think of replacing the jokers w...
New
bjorng
This topic is about Day 10 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adv...
New
sasajuric
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 an...
New
bjorng
This topic is about Day 17 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
Aetherus
This topic is about Day 7 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
woolfred
It is that time of the year again: Advent of Code 2022 :christmas_tree: Day 1 Leaderboard:
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement