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:

The join code is:

Merry Christmas everyone :tada:

1 Like

Merry Christmas everyone!

Here is my solution.


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

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

  def run2() do
    "Merry Xmas !"

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

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

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
    |> loop_size(@subject_number)
    |> encryption_key(door_key)

  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
    |> transform(subject)
    |> loop_size(key, subject, loop + 1)

  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
    |> transform(subject)
    |> encryption_key(subject, loops - 1)

  def transform(value, subject) do
    rem(value * subject, @magic_number)
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: 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)
1 Like