Skip to content

Instantly share code, notes, and snippets.

@luissimas
Last active June 30, 2021 21:59
Show Gist options
  • Save luissimas/499eb83f340987c9625fc153ab004a5e to your computer and use it in GitHub Desktop.
Save luissimas/499eb83f340987c9625fc153ab004a5e to your computer and use it in GitHub Desktop.
Advent of code - Day 1 (IFP)
defmodule Advent1 do
def solution([]), do: nil
def solution([head | tail]) do
filtered = Enum.filter(tail, fn element -> head + element == 2020 end)
case filtered do
[] -> solution(tail)
_ -> head * hd(filtered)
end
end
end
IO.puts("Advent of Code Day 1 2020")
input1 = [1721, 979, 366, 299, 675, 1456]
input2 = [2010, 123_904_120, 21093, 1_239_402_013, 1239, 10, 34289]
input3 = [1, 123_904_120, 21093, 1_239_402_013, 2019, 10, 34289]
IO.puts("The first input list is #{inspect(input1)}")
IO.puts("The result for the first input is #{inspect(Advent1.solution(input1))}")
IO.puts("The second input list is #{inspect(input2)}")
IO.puts("The result for the second input is #{inspect(Advent1.solution(input2))}")
IO.puts("The third input list is #{inspect(input3)}")
IO.puts("The result for the third input is #{inspect(Advent1.solution(input3))}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment