bjorng
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
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
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.
As I was writing this, Aetherus’s post appeared and I realized I forgot about the :discard option on chunk_every… oh well.
sasajuric
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
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
My Day4 Solution
Any feedbacks are welcome! 







