Advent of code 2020 - Day 25

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

Merry Christmas everyone :tada:

1 Like

Merry Christmas everyone!

Here is my solution.

2 Likes

Thanks a lot, I learned a lot by reading your solutions and all !

Merry Xmas !

defmodule AOC.D25 do
  import AOC.Helper.Input

  def run1(test \\ false) do
    [cpubk, dpubk] =
      get_input("D25", test)
      |> split_input()
      |> Enum.map(&String.to_integer/1)

    transforms(cpubk, 7, 1, 1)
    |> encryption_key(0, dpubk, 1)
  end

  def run2() do
    "Merry Xmas !"
  end

  def encryption_key(lsize, count, _subject, value) when lsize == count, do: value

  def encryption_key(lsize, count, subject, value) do
    encryption_key(lsize, count + 1, subject, rem(value * subject, 20_201_227))
  end

  def transforms(pubkey, _subject, lsize, value) when pubkey == value, do: lsize - 1

  def transforms(pubkey, subject, lsize, value) do
    transforms(pubkey, subject, lsize + 1, rem(value * subject, 20_201_227))
  end
end
3 Likes

Merry Xmas everyone, thanks a lot for all the fun!

Also went back to redo Day20 part 2 from scratch to get the last star, here is todays.

defmodule AdventOfCode.Day25 do
  @subject_number 7
  @magic_number 20_201_227

  def run({card_key, door_key}) do
    card_key
    |> loop_size(@subject_number)
    |> encryption_key(door_key)
  end

  def loop_size(key, subject), do: loop_size(1, key, subject, 0)

  def loop_size(value, key, _subject, loop) when key == value, do: loop

  def loop_size(value, key, subject, loop) do
    value
    |> transform(subject)
    |> loop_size(key, subject, loop + 1)
  end

  def encryption_key(loops, subject), do: encryption_key(1, subject, loops)

  def encryption_key(value, _subject, 0), do: value

  def encryption_key(value, subject, loops) do
    value
    |> transform(subject)
    |> encryption_key(subject, loops - 1)
  end

  def transform(value, subject) do
    rem(value * subject, @magic_number)
  end
end
1 Like

My code is a bit long. Is started to implement each step separately because the part 1 text gave all what there is to be done to solve the problem, so I expected a part 2 this year.

Last year was my first AoC and I found it really hard. Day 25 was about finding your way in a spaceship, on a map that could not fit on a grid (if you do east -> south -> west -> north you could be somewhere else than your starting point), while carrying objects to change your weight to open a gate. And doing so not directly in code, but by inputting plaintext commands to an intcode program.

And this year you have to increment a number until rem(the_number * 7, 20_201_227) is equal to your input. Oh and the number is not like 20020024857472872873874… mine was 8.

I have to admit that I am kind of disappointed. I expected to struggle way more.

But still, it was fun anyway, and I learned new techniques along the way, by myself and by looking at the code posted here.

Merry Christmas to you all.

I was so tired on D25 that I stubbornly held on to some ideas that performed pretty badly. I got the answer, but was pretty dissatisfied. However today I refactored it to be quite nice. I love Stream.iterate :heart:

Merry christmas everyone.

1 Like

Here’s mine: https://twitter.com/mafinar/status/1343609407201042436 couldn’t resist fitting it in a tweet.

defmodule Day25 do
  def run(a, b), do: t(1, a, lp(1, b, 1))

  def lp(x, k, s), do: ((r = t1(x, 7)) == k && s) || lp(r, k, s + 1)

  def t(x, p, s), do: (s == 0 && x) || t(t1(x, p), p, s - 1)
  def t1(x, p), do: rem(x * p, 20_201_227)
end
1 Like