bjorng

bjorng

Erlang Core Team

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

First Post!

Most Liked

epilgrim

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

epilgrim

epilgrim

My solution

Today was significatively simpler than yesterday, but I had to remember my vector algebra :slight_smile:
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
ruslandoga

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...

Last Post!

unsek

unsek

I just uploaded Jose’s day 5 stream summary: https://www.youtube.com/watch?v=K6SFEoRaTNE. Hope this summary could be of use for anyone. :smiley:

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews