diff --git a/day1/README b/day1/README index dd11edb..4269f0d 100644 --- a/day1/README +++ b/day1/README @@ -32,3 +32,10 @@ E.g. However, can't short-circuit comprehensions, so maybe recursion was a good choice after all 🤷‍♂️ +Tried it out, and turned out that while it doesn't short-circuit, it's still pretty quick. So I added +day1refactored.exs with the simpler implementation. + +$ elixir day1refactored.exs +Part1: 618 x 1402 = 866436 +Part2: 928 x 547 x 545 = 276650720 + diff --git a/day1/day1refactored.exs b/day1/day1refactored.exs new file mode 100644 index 0000000..4184c66 --- /dev/null +++ b/day1/day1refactored.exs @@ -0,0 +1,11 @@ +list = + File.read!("input") + |> String.trim() + |> String.split("\n") + |> Enum.map(&String.to_integer/1) + +[{a, b} | _] = for i <- list, j <- list, i + j == 2020, do: {i, j} +IO.puts("Part1: #{a} x #{b} = #{a * b}") + +[{a, b, c} | _] = for i <- list, j <- list, k <- list, i + j + k == 2020, do: {i, j, k} +IO.puts("Part2: #{a} x #{b} x #{c} = #{a * b * c}")