Aetherus
Most Liked
hauleth
My approach to make it not only fast, but also clean and readable:
defmodule Solution do
def read(path) do
path
|> File.stream!()
|> Enum.map(&String.trim/1)
|> Enum.map(&parse/1)
end
defp parse(input) do
[spec, pass] = String.split(input, ": ", parts: 2)
[range, <<char>>] = String.split(spec, " ", parts: 2)
[min, max] =
range
|> String.split("-", parts: 2)
|> Enum.map(&String.to_integer/1)
{min..max, char, pass}
end
def validate_1({range, char, pass}) do
count = for <<^char <- pass>>, reduce: 0, do: (n -> n + 1)
count in range
end
def validate_2({a..b, char, pass}) do
<<char_1>> = binary_part(pass, a - 1, 1)
<<char_2>> = binary_part(pass, b - 1, 1)
char_1 != char_2 and char in [char_1, char_2]
end
end
data = Solution.read("2/input.txt")
IO.inspect(Enum.count(data, &Solution.validate_1/1), label: "task 1")
IO.inspect(Enum.count(data, &Solution.validate_2/1), label: "task 2")
6
bossek
Following should work:
(String.at(password, i - 1) == char) != (String.at(password, j - 1) == char)
5
dams
Short solution part 1:
File.stream!("input")
|> Stream.filter(fn str ->
[_, min, max, char, pass] = Regex.run(~r/^(\d+)-(\d+) (.): (\S+)/, str)
count = String.graphemes(pass) |> Enum.frequencies() |> Map.get(char, 0)
count >= String.to_integer(min) && count <= String.to_integer(max)
end)
|> Enum.count()
|> IO.puts()
5
Popular in Challenges
Hey there :wave:
No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
This one has been quite the ride. Struggled at first to find a good data format to suite the problem. I really like how that turned out b...
New
Took me a minute to remember my binary math :smile: :grimacing:..
import Bitwise
__DIR__
|> Path.join("puzzle.txt")
|> File.strea...
New
Note: This topic is to talk about Day 12 of the Advent of Code.
For general discussion about the Advent of Code 2018 and links to topics...
New
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
New
This one was much easier for me than yesterday’s. Part 1 runs in 22ms and part 2 runs in ~3s.
defmodule BridgeRepair do
def parse_inpu...
New
Nobody’s doing Advent of Code this year? :grinning_face_with_smiling_eyes:
I might do the first week or so.
For Day 1, first I solved i...
New
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
Thought I’d kick today’s thread off!
Parsing
Enum rocks, so most of my code was actually in parsing input.
▶ Preprocessing input
Part 1...
New
Other popular topics
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
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








