Papey

Papey

Avent of Code 2020 - Day 20

This topic is about Day 20 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

Papey

Papey

Just the part 1 for now.

I did not reconstruct the map, just looked at corners. It’s ugly and verbose but it works.

Now on part 2, but I already find the sea monster, it’s my function that finds corners :sweat_smile:

  def find_corners(tiles) do
    # Map all edges to corresponding tiles
    Enum.reduce(tiles, %{}, fn {id, tile}, acc ->
      edges = edges(tile)

      Enum.reduce(edges ++ Enum.map(edges, &flip/1), acc, fn edge, acc ->
        {_old, acc} =
          Map.get_and_update(acc, edge, fn old ->
            if old do
              {old, [id | old]}
            else
              {old, [id]}
            end
          end)

        acc
      end)
    end)
    # Filters out singleton
    |> Enum.filter(fn {_edge, ids} -> length(ids) == 1 end)
    # A list of one is just the element inside that list
    |> Enum.map(fn {edge, [id]} -> {edge, id} end)
    # Reverse the reduce to find how many edges maps this tile id
    |> Enum.reduce(%{}, fn {_edge, id}, acc ->
      {_old, acc} =
        Map.get_and_update(acc, id, fn old ->
          if old do
            {old, old + 1}
          else
            {old, 1}
          end
        end)

      acc
    end)
    # filter out candidates
    |> Enum.filter(fn {_id, match} -> match > 2 && match < 5 end)
    |> Enum.map(fn {id, _match} -> id end)
  end
lud

lud

I got is working this afternoon, and I’ve just cleaned the code a little bit.

Not very proud of this solution as it is messy and hard to read. But it completes in 15ms (not counting transforming the raw input into list of tiles (which are lists of lists of chars).

bjorng

bjorng

Erlang Core Team

I tried to leverage my corner-finding code from part 1 for part 2, but I didn’t make much progress. After sleeping on it I decided to start over with another approach for part 2. I spent most of the time getting the tile combining to work. After that, it was fairly easy to implement the search for the sea monster.

Here is my solution.

blue_quartz

blue_quartz

Not sure if it’s too late, but here’s my day 20 solution if anyone’s interested.

advent20/day20.ex at master · h-j-k/advent20 (github.com)

I’ve also documented my walkthrough if you want to understand the logic behind the steps I’ve taken… advent20/README.md at master · h-j-k/advent20 (github.com)

Where Next?

Popular in Challenges Top

Aetherus
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
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
New
sukhmeetsd
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
bjorng
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
jkwchui
Monkeys fitted squarely as GenServers in my head. My initial problem was using cast instead of call; I imagine impolite monkeys slinging...
New
code-shoily
Just did part 1. Part 2 seems to be demanding too much of my reading time so will get to that after I am done with some chores. Oh here ...
New
New
adamu
Probably not the most efficient implementation, because part 1 took &gt;1 ms and part 2 &gt;4ms, but the code was simple enough. def p...
New
liamcmitchell
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vac
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement