bjorng
Erlang Core Team
Advent of Code 2021 - Day 5
This topic is about Day 5 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adventofcode.com/2021/leaderboard/private/view/370884
The entry code is:
370884-a6a71927
Most Liked
epilgrim
maybe you will like my way of parsing the input?
File.read!("day_5.txt")
|> String.split(["\n", ",", " -> "], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4)
|> Enum.map(&List.to_tuple/1)
We can split the file in all the special content we have, and then just take numbers 4 at a time. No need to nest anything
2
epilgrim
My solution
Today was significatively simpler than yesterday, but I had to remember my vector algebra 
To calculate the intermediate points, I used:
def fill_points({x1,y1,x2,y2}) do
{dx, dy} = {x2 - x1, y2 - y1}
slope = {step(dx), step(dy)}
do_fill({x1, y1}, {x2, y2}, slope, [])
end
defp step(0), do: 0
defp step(x) when x > 0, do: 1
defp step(x) when x < 0, do: -1
defp do_fill(point, point, _slope, acc) do
[point | acc]
end
defp do_fill({x1, y1} = point, end_point, {dx, dy} = slope, acc) do
new_point = {x1 + dx, y1 + dy}
do_fill(new_point, end_point, slope, [point | acc])
end
1
ruslandoga
IO.puts would work, I think:
iex(5)> input = "1.1...11.\n.111...2...\n...2.1.111.\n...1.2.2...\n.112313211\n...1.2...\n...1...1...\n.1...1...\n1...1.\n222111..."
iex(6)> IO.puts input
1.1...11.
.111...2...
...2.1.111.
...1.2.2...
.112313211
...1.2...
...1...1...
.1...1...
1...1.
222111...
1
qhwa
Here’s my take:
defmodule Y2021.Day05 do
def p1 do
AOC.Input.stream("2021/day05.txt", &parse_seg/1)
|> Stream.filter(fn [x1, y1, x2, y2] -> x1 == x2 || y1 == y2 end)
|> count_overlap()
end
def p2 do
AOC.Input.stream("2021/day05.txt", &parse_seg/1)
|> count_overlap()
end
defp parse_seg(line) do
Regex.run(~r/(\d+),(\d+) -> (\d+),(\d+)/, line, capture: :all_but_first)
|> Enum.map(&String.to_integer/1)
end
defp count_overlap(segs) do
segs
|> Stream.flat_map(fn [x1, y1, x2, y2] ->
max_step = max(abs(x2 - x1), abs(y2 - y1))
dx = div(x2 - x1, max_step)
dy = div(y2 - y1, max_step)
Stream.iterate({x1, y1}, fn {x, y} -> {x + dx, y + dy} end)
|> Stream.take(max_step + 1)
|> Enum.to_list()
end)
|> Enum.frequencies()
|> Enum.count(fn {_, count} -> count > 1 end)
end
end
1
Klohto
Wow, today was an absolute breeze (looking at you Day 4…)
- Learned about pairing Stream.cycle with Enum.zip
- Had to switch from Livebook native input (looks like it’s deprecated at
edge) to Kino input - Pattern matching makes this easy
- Just expanded all coordinates and counted frequencies bigger than 1
# Day 4
## Deps & preparation
```elixir
Mix.install([
{:kino, "~> 0.4.0"}
])
input = Kino.Input.textarea("Please paste your input file:")
```
## Input
```elixir
coordinates =
Kino.Input.read(input)
|> String.split(["\n", ",", " -> "], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4)
```
## Part 1
```elixir
coordinates
|> Enum.reduce(
[],
fn
[x, y, x1, y1], map when y == y1 ->
Enum.zip(x..x1, Stream.cycle([y])) ++ map
[x, y, x1, y1], map when x == x1 ->
Enum.zip(Stream.cycle([x]), y..y1) ++ map
_, map ->
map
end
)
|> Enum.frequencies()
|> Enum.count(fn {_k, v} -> v > 1 end)
```
## Part 2
```elixir
coordinates
|> Enum.reduce(
[],
fn
[x, y, x1, y1], map when y == y1 ->
Enum.zip(x..x1, Stream.cycle([y])) ++ map
[x, y, x1, y1], map when x == x1 ->
Enum.zip(Stream.cycle([x]), y..y1) ++ map
[x, y, x1, y1], map ->
Enum.zip(x..x1, y..y1) ++ map
end
)
|> Enum.frequencies()
|> Enum.count(fn {_k, v} -> v > 1 end)
```
EDIT: Updated with @epilgrim split approach, thanks!
1
Popular in Challenges
Today is a brute-force day: advent-of-code-2024/lib/advent_of_code2024/day6.ex at main · ibarakaiev/advent-of-code-2024 · GitHub
Takes a...
New
Note: This topic is to talk about Day 25 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
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
This was way too easy after the last few days. Simple map, filter and count.
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution:
New
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
Note: This topic is to talk about Day 5 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]...
New
This topic is about Day 9 of the Advent of Code 2021 .
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Other popular topics
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
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







