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

Showing Posts 1 to 10

stevensonmt

stevensonmt

This one was nicer to me. I like my approach for handling the lines, but I don’t like my approach to parsing the input. There has to be a pattern matching on binaries but it’s beyond me.

https://github.com/stevensonmt/advent_of_code/blob/ace53043d4d95ffdb60f0d47c396f87a6f5562a2/2021/day5/lib/day5.ex

APB9785

APB9785

Creator of ECSx
def part_1 do
  @path
  |> parse_input()
  |> Enum.filter(fn line -> line_direction(line) in @orthogonal end)
  |> plot_lines_to_map()
  |> Enum.count(fn {_k, v} -> v > 1 end)
end

Part 2 simply removes the Enum.filter
Full solution here

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

code-shoily

code-shoily

This one was fun. Beautiful use of pipe and pattern:

defmodule AdventOfCode.Y2021.Day05 do
  use AdventOfCode.Helpers.InputReader, year: 2021, day: 5

  def run_1, do: input!() |> parse() |> overlaps(false)
  def run_2, do: input!() |> parse() |> overlaps(true)
  def parse(data), do: Enum.map(String.split(data, "\n"), &ranges/1)

  defp ranges(line) do
    ~r/(\d+),(\d+) -> (\d+),(\d+)/
    |> Regex.run(line, capture: :all_but_first)
    |> Enum.map(&String.to_integer/1)
    |> Enum.split(2)
  end

  defp overlaps(ranges, diagonal?) do
    ranges
    |> Enum.flat_map(&points_between(&1, diagonal?))
    |> Enum.frequencies()
    |> Enum.count(&(elem(&1, 1) >= 2))
  end

  defp points_between({from, to}, diagonal?) do
    case {{from, to}, diagonal?} do
      {{[same, a], [same, b]}, _} -> Enum.map(a..b, &{same, &1})
      {{[a, same], [b, same]}, _} -> Enum.map(a..b, &{&1, same})
      {range, true} -> diagonals(range)
      _ -> []
    end
  end

  defp diagonals({[a, b], [c, d]}),
    do: Enum.map(0..abs(a - c), &{(a > c && a - &1) || a + &1, (b > d && b - &1) || b + &1})
end
weego

weego

Feels regex is justified here, maybe?

defmodule Aoc2021.Vents do
  def overlaps(coords) do
    regex = ~r/(\d+),(\d+) -> (\d+),(\d+)/

    coords
    |> String.split("\n", trim: true)
    |> Enum.map(fn line ->
      Regex.run(regex, line, capture: :all_but_first)
      |> Enum.map(&String.to_integer/1)
      |> then(fn [x1, y1, x2, y2] -> [{x1, y1}, {x2, y2}] end)
    end)
    |> Enum.flat_map(&expand/1)
    |> Enum.frequencies()
    |> Enum.count(fn {_key, val} -> val > 1 end)
  end

  defp expand([{x1, y1}, {x2, y2}]) do
    cond do
      x1 == x2 -> for y <- y1..y2, do: {x1, y}
      y1 == y2 -> for x <- x1..x2, do: {x, y1}
      abs(y1 - y2) == abs(x1 - x2) -> Enum.zip(x1..x2, y1..y2)
      :otherwise -> []
    end
  end
end

Malian

Malian

Here is my solution for part 1. For part two, removing the filter does the job.

expend_lines = fn
  [{x, y1}, {x, y2}] ->
    y = y1..y2
    Enum.zip(List.duplicate(x, Enum.count(y)), y1..y2)

  [{x1, y}, {x2, y}] ->
    x = x1..x2
    Enum.zip(x1..x2, List.duplicate(y, Enum.count(x)))

  [{x1, y1}, {x2, y2}] ->
    Enum.zip(x1..x2, y1..y2)
end


input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  line
  |> String.split([",", "->"], trim: true)
  |> Enum.map(fn value -> String.trim(value) |> String.to_integer() end)
  |> Enum.chunk_every(2)
  |> Enum.map(&List.to_tuple/1)
end)
|> Enum.filter(fn [{x1, y1}, {x2, y2}] -> x1 == x2 or y1 == y2 end)
|> Enum.flat_map(expend_lines)
|> Enum.frequencies()
|> Enum.count(fn {_, count} -> count > 1 end)

I also draw the board in order to debug, but I am not able to properly draw it neither in console neither in Live Book due to \n that are not “expended”.

“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…”

Someone knows how I can show it like this?

1.1....11.
.111...2..
..2.1.111.
...1.2.2..
.112313211
...1.2....
..1...1...
.1.....1..
1.......1.
222111....
``
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...

Where Next? Top

Trending in Challenges Top

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews