hvnsweeting

hvnsweeting

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

Most Liked

bjorng

bjorng

Erlang Core Team

Merry Christmas everyone!

Here is my solution.

Papey

Papey

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
Hallski

Hallski

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

code-shoily

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
hvnsweeting

hvnsweeting

Merry Christmas everyone :tada:

Where Next?

Popular in Challenges Top

NobbZ
Note by the Moderators: This topic is to talk about the Day 2 of the Advent of Code. For general discussion about the Advent of Code 201...
New
bjorng
Note: This topic is to talk about Day 25 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
New
bjorng
This topic is about Day 17 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
gangstead
This is my second year doing AoC in Elixir and my first year doing it with Livebook. When I was doing just plain Elixir I usually set up...
New
New
bjorng
This topic is about Day 6 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
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

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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