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
Not the prettiest but it works
https://github.com/rugyoga/aoc2023/blob/main/lib/2024/9.ex
New
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
Continuation of Advent of Code 2022:christmas_tree:, Day 1:
Day 2!
Leaderboard:
New
I have 2 arrays: a1 can be any combination of value or nil like that
a1 = [1,nil,3]
and array 2 the same
a2 = [4,2, nil]
How do I com...
New
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
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...
New
My solution finishes both parts in 5 seconds on my computer. That time should be possible to reduce by optimizing my rather naive tilt/2 ...
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Hello everyone,
I’m a new in elexir and functional language. I’m trying to implement Websocket interraction with server.
On first layer...
New
Other popular topics
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
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
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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








