this repo has no description
1defmodule Solution do 2 def load(path) do 3 path 4 |> File.stream!() 5 |> Stream.map(&String.to_integer(String.trim(&1))) 6 |> Enum.to_list() 7 end 8 9 def run([], _, _), do: nil 10 def run(_, _, sum) when sum < 0, do: nil 11 def run([a | _], 1, a), do: [a] 12 13 def run([a | rest], n, sum) do 14 case run(rest, n - 1, sum - a) do 15 nil -> run(rest, n, sum) 16 nums when is_list(nums) -> [a | nums] 17 end 18 end 19end 20 21list = Solution.load("./1/input.txt") 22 23list 24|> Solution.run(2, 2020) 25|> Enum.reduce(&*/2) 26|> IO.inspect(label: "task 1") 27 28list 29|> Solution.run(3, 2020) 30|> Enum.reduce(&*/2) 31|> IO.inspect(label: "task 2") 32 33Benchee.run(%{ 34 "first solution" => fn -> 35 try do 36 for x <- list, y <- list, z <- list, x + y + z == 2020 do 37 throw(x * y * z) 38 end 39 40 :error 41 catch 42 num -> {:ok, num} 43 end 44 end, 45 "optimized" => fn -> 46 try do 47 for x <- list, 48 rest = 2020 - x, 49 y <- list, 50 y < rest, 51 rest = 2020 - x - y, 52 z <- list, 53 z <= rest, 54 x + y + z == 2020, 55 do: throw(x * y * z) 56 57 :error 58 catch 59 num -> {:ok, num} 60 end 61 end, 62 "mine" => fn -> 63 Solution.run(list, 3, 2020) 64 end 65})