Day 1: Chronal Calibration - Advent of Code 2018

I did the Advent of Code 2018 day 1 challenge in Elixir! Parts one and two are as follows:

defmodule Day1 do
  def frequency(input) do
    input
    |> read_input()
    |> Enum.reduce(0, fn line, acc ->
      String.to_integer(line) + acc
    end)
  end

  def repeated_frequency(input) do
      input
      |> read_input()
      |> Stream.cycle()
      |> Enum.reduce_while({0, MapSet.new([0])}, fn line, {acc, seen} ->
        value = String.to_integer(line) + acc
        if MapSet.member?(seen, value) do
          {:halt, value}
        else
          {:cont, {value, MapSet.put(seen, value)}}
        end
      end)
  end

  defp read_input(input) do
    input
    |> File.read!()
    |> String.split("\n", trim: true)
  end
end

:aoc2018 |> :code.priv_dir() |> Path.join("day1.txt") |> Day1.frequency()
:aoc2018 |> :code.priv_dir() |> Path.join("day1.txt") |> Day1.repeated_frequency()

 

 

Author

Simon Escobar Benitez

Colombian Software Engineer (Erlang Solutions)

Upcoming conferences

Start booking your calendar with more Code Sync conferences happening across the globe. We will be slowly releasing more dates, in the meantime here is what we’ve planned already:

All conferences

ARTICLES: 5

Day 10: The Stars Align - Advent of Code 2018
Simon Escobar Benitez

I did the Advent of Code 2018 day 10 challenge in Elixir! Parts one and two are as follows:

READ MORE
  • Elixir
  • Advent of Code
Day 5: Alchemical Reduction - Advent of Code 201
Simon Escobar Benitez

I did the Advent of Code 2018 day 5 challenge in Elixir! Parts one and two are as follows:

READ MORE
  • Elixir
  • Advent of Code
Day 4: Repose Record - Advent of Code 2018
Simon Escobar Benitez

I did the Advent of Code 2018 day 4 challenge in Elixir! Parts one and two are as follows:

READ MORE
  • Elixir
  • Advent of Code
Day 3: No matter how you slice it - Advent of Code 2018
Simon Escobar Benitez

I did the Advent of Code 2018 day 3 challenge in Elixir! Parts one and two are as follows:

READ MORE
  • Elixir
  • Advent of Code
Day 2: Inventory Management System - Advent of Code 2018
Simon Escobar Benitez

I did the Advent of Code 2018 day 2 challenge in Elixir! Parts one and two are as follows:

READ MORE
  • Elixir
  • Advent of Code