Powered by AppSignal & Oban Pro

Day 10 - Advent of Code 2015

lib/advent_of_code/2015/day-10.livemd

Day 10 - Advent of Code 2015

Mix.install([:kino, :benchee])
:ok

Links

Prompt

— Day 10: Elves Look, Elves Say —

Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as “one two, two ones”, which becomes 1221 (1 2, 2 1s).

Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1).

For example:

  • 1 becomes 11 (1 copy of digit 1).
  • 11 becomes 21 (2 copies of digit 1).
  • 21 becomes 1211 (one 2 followed by one 1).
  • 1211 becomes 111221 (one 1, one 2, and two 1s).
  • 111221 becomes 312211 (three 1s, two 2s, and one 1).

Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result?

To begin, get your puzzle input.

Your puzzle input is 1321131112.

— Part Two —

Neat, right? You might also enjoy hearing John Conway talking about this sequence (that’s Conway of Conway’s Game of Life fame).

Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of the new result?

Although it hasn’t changed, you can still get your puzzle input.

Your puzzle input was 1321131112.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"1321131112"

Solution

digits =
  input
  |> String.to_integer()
  |> Integer.digits()

convert = fn
  [a | [a | _] = tail], {count, acc}, fun ->
    fun.(tail, {count + 1, acc}, fun)

  [a | tail], {count, acc}, fun ->
    fun.(tail, {0, [a | [count + 1 | acc]]}, fun)

  [], {_, acc}, _ ->
    Enum.reverse(acc)
end

calc_length = fn input, n ->
  for _ <- 1..n, reduce: input do
    acc -> convert.(acc, {0, []}, convert)
  end
  |> length()
end
#Function<41.3316493/2 in :erl_eval.expr/6>

QUESTION ONE?

Your puzzle answer was answer one.

calc_length.(digits, 40)
492982

QUESTION TWO?

Your puzzle answer was answer two.

calc_length.(digits, 50)

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.