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 17 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
This topic is about Day 3 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 5 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adve...
New
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
Fairly straightforward Dijkstra’s algorithm
import AOC
aoc 2023, 17 do
def compute(input, candidates) do
{{max_row, max_col}, ite...
New
This topic is about Day 15 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
Hi, there :wave:
Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization ...
New
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
can someone please explain to me how Enum.reduce works with maps
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
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
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







