Advent of Code (2016) : Day 9

-- Problem --
You can find Part 1 of the problem statement here. Part 2 is unlocked on successful completion of Part 1.

-- Solution in Elixir --

       defmodule Aoc.Day9 do

  def parse_input() do
    {:ok, input} = File.read("day9_input.txt")
    String.trim_trailing(input)
  end

  def decompress_1("", result), do: result
  def decompress_1("(" <> tail, result) do
    [str_take, str_repeat, rest] = String.split(tail, ["x", ")"], parts: 3)
    {num_take, num_repeat} = {String.to_integer(str_take), String.to_integer(str_repeat)}
    {to_repeat, remaining} = String.split_at(rest, num_take)
    partial_result = String.duplicate(to_repeat, num_repeat)
    decompress_1(remaining, result <> partial_result)
  end

  def decompress_1(<< head >> <> tail, result) do
    decompress_1(tail, result <> << head >>)
  end

  def output_1() do
    parse_input()
    |> decompress_1("")
    |> String.length
  end

  def decompress_2("", count), do: count
  def decompress_2("(" <> tail, count) do
    [str_take, str_repeat, rest] = String.split(tail, ["x", ")"], parts: 3)
    {num_take, num_repeat} = {String.to_integer(str_take), String.to_integer(str_repeat)}
    {to_repeat, remaining} = String.split_at(rest, num_take)
    partial_result = String.duplicate(to_repeat, num_repeat)
    decompress_2(partial_result <> remaining, count)
  end

  def decompress_2(<< head >> <> tail, count) do
    decompress_2(tail, count + 1)
  end

  def output_2() do
    parse_input()
    |> decompress_2(0)
  end

end
    

You can find all my Advent of Code (2016) solutions here.