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
hauleth
Mostly what @stevensonmt said. The pin operator is needed to filter characters, as generator will allow only matches, in other words it is the same as:
for <<c <- pass>>, match?(^char, c), …
Which in the end behaves exactly the same as:
for <<c <- pass>>, char == c, …
But is shorter and a little bit more confusing definition of such behaviour.
The rest is new, as you have spotted, syntax for defining reduction. In short:
for item <- generator, reduce: x, do: (n -> …)
Is the same as:
Enum.reduce(generator, x, fn item n -> … end)
So in the end it just count characters that match given byte.
5
Damirados
Seems no one stumbled upon :erang.xor 
defmodule Event2 do
def run do
IO.puts("Test part1: #{part1("input/event2/test.txt")}")
IO.puts("Puzzle part1: #{part1("input/event2/puzzle.txt")}")
IO.puts("Test part2: #{part2("input/event2/test.txt")}")
IO.puts("Puzzle part2: #{part2("input/event2/puzzle.txt")}")
end
def part1(path), do: input_stream(path) |> Stream.filter(&filter_fun/1) |> Enum.count()
def part2(path), do: input_stream(path) |> Stream.filter(&filter_fun2/1) |> Enum.count()
def input_stream(path), do: path |> File.stream!() |> Stream.map(&parse_input/1)
def parse_input(input) do
[low, high, letter, pass] = String.trim(input) |> String.split(~r/[-: ]/, trim: true)
{String.to_integer(low), String.to_integer(high), letter, pass}
end
def filter_fun({low, high, letter, pass}) do
pass_letter_count = pass |> String.graphemes() |> Enum.count(&(&1 == letter))
low <= pass_letter_count and pass_letter_count <= high
end
def filter_fun2({low, high, letter, pass}),
do: :erlang.xor(String.at(pass, low - 1) == letter, String.at(pass, high - 1) == letter)
end
4
Popular in Challenges
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
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
This topic is about Day 8 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
The second part of today’s puzzle is very misleading.
FYI, each of the ghosts has only one possible position that ends with a "Z" on its...
New
This topic is about Day 2 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adven...
New
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
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
Today’s problem is really tense. I don’t think I can do it without libgraph.
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
Hi, there :wave:
Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
Other popular topics
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
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
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
I would like to know what is the best IDE for elixir development?
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New








