Add simpler implementation for Day1

This commit is contained in:
Adam Millerchip 2020-12-01 16:48:01 +09:00
parent e4af8d0370
commit c50feb2380
2 changed files with 18 additions and 0 deletions

View File

@ -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

11
day1/day1refactored.exs Normal file
View File

@ -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}")