bjorng

bjorng

Erlang Core Team

Advent of Code 2020 - Day 17

This topic is about Day 17 of the Advent of Code 2020 .

Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/leaderboard/private/view/39276

The join code is:
39276-eeb74f9a

Most Liked

Hallski

Hallski

They updated the text slightly after a while since a lot of people got confused by the example (me included). In short, the top left coordinate in the z0 layer is not the same in each iteration and moves around depending on where there are active satellites.

Hallski

Hallski

Struggled for about an hour trying to make sense of the examples (and thought they meant the space wrapped around which created a real headache with a growing universe :slight_smile:).

I settled for storing the active satellites in a map, keyed on coordinates. Then I ran each iteration and checked all positions within my active state plus one in each direction.

Here is my solution.

Part 2 was simply extending part 1 with an extra dimension so only posted that.

@bjorng nice to use Stream.iterate() |> Enum.drop(6), also realised reading your code that I was a bit silly to filter my Map and use Enum.count since I also only store the active satellites in my map.

mruoss

mruoss

Oh boy, I get it now! I was looking for something in the instructions, but it’s how the examples are displayed. The frame of view is moving!
I could have ignored the examples but it bothered me that my understanding after which I write the algorithm wouldn’t even comply with the example. Alright… let’s see where to find the motivation to solve this now… :joy:

lud

lud

Hi @faried

First thing is you can actually have multiple generators in your for construct, so you do not have to flatten:

  def neighbors3({x, y, z}) do
    for i <- -1..1,
        j <- -1..1,
        k <- -1..1 do
      {x + i, y + j, z + k}
    end
    # we have to return the neighbors of a point, so we explicitly
    # remove the point from the output
    |> Enum.reject(fn {a, b, c} -> a == x and b == y and c == z end)
  end

The best part is that you can still pull values out of a previous generated value, for example:

for values <- rows, value <- values, do: something(value)

Now, you need to use Enum.reject is simply because at one moment of the iteration, i is 0, j is 0 and k is also 0, so your expression {x + i, y + j, z + k} does not represent a neighbour but the central element itself.

That is why you have to “explicitly remove the point from the output” as you have said.

So you could filter-out the central point directly in the generator (in you case, that would be in the most inner for.

  def neighbors3({x, y, z} = coords) do
    for i <- -1..1,
        j <- -1..1,
        k <- -1..1,
        n = {x + i, y + j, z + k},
        n != coords do              # <-- accept only when !=coords
      n
    end
  end

What I did today, though, is directly count the numbers of neighbours, with a for + reduce loop. My map contains 0 for inactive cubes and 1 for active ones.

  defp active_neighbours_count(map, {x, y, z} = coords) do
    for nx <- (x - 1)..(x + 1),
        ny <- (y - 1)..(y + 1),
        nz <- (z - 1)..(z + 1),
        {nx, ny, nz} != coords,
        reduce: 0 do
      acc ->
        Map.get(map, {nx, ny, nz}, 0) + acc
    end
  end
faried

faried

Thanks. I have used multiple generators with for in the past – I just didn’t think of it this time. I don’t work with Elixir on a day-to-day basis. One of the reasons I’m trying to do them all in Elixir this year is to learn from other solutions.

The Enum.reject I mentioned is in my cycle function, where I remove cubes that have no neighbors. That’s what fixes my off-by-one error. I had a lot of trouble figuring out why I need that.

Where Next?

Popular in Challenges Top

sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
sasajuric
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
LostKobrakai
This one has been quite the ride. Struggled at first to find a good data format to suite the problem. I really like how that turned out b...
New
Aetherus
This topic is about Day 5 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
bjorng
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
bjorng
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
kwando
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile: The trick was to first generate a list...
New
christhekeele
Thought I’d kick today’s thread off! Parsing Enum rocks, so most of my code was actually in parsing input. ▶ Preprocessing input Part 1...
New

Other popular topics Top

JDanielMartinez
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
mgjohns61585
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement