From eb3a9bbe535f8a64c7693a3e92c98831d453abd4 Mon Sep 17 00:00:00 2001 From: Garrit Franke Date: Mon, 29 Nov 2021 10:28:54 +0100 Subject: [PATCH] Solve 2020 day 1 part 1 in elixir --- 2020/Day1/Elixir/.formatter.exs | 4 + 2020/Day1/Elixir/.gitignore | 26 ++++ 2020/Day1/Elixir/README.md | 21 +++ 2020/Day1/Elixir/input.txt | 200 ++++++++++++++++++++++++++ 2020/Day1/Elixir/lib/day1.ex | 34 +++++ 2020/Day1/Elixir/mix.exs | 28 ++++ 2020/Day1/Elixir/test/day1_test.exs | 8 ++ 2020/Day1/Elixir/test/test_helper.exs | 1 + 8 files changed, 322 insertions(+) create mode 100644 2020/Day1/Elixir/.formatter.exs create mode 100644 2020/Day1/Elixir/.gitignore create mode 100644 2020/Day1/Elixir/README.md create mode 100644 2020/Day1/Elixir/input.txt create mode 100644 2020/Day1/Elixir/lib/day1.ex create mode 100644 2020/Day1/Elixir/mix.exs create mode 100644 2020/Day1/Elixir/test/day1_test.exs create mode 100644 2020/Day1/Elixir/test/test_helper.exs diff --git a/2020/Day1/Elixir/.formatter.exs b/2020/Day1/Elixir/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/2020/Day1/Elixir/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/2020/Day1/Elixir/.gitignore b/2020/Day1/Elixir/.gitignore new file mode 100644 index 0000000..550c6d5 --- /dev/null +++ b/2020/Day1/Elixir/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +day1-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/2020/Day1/Elixir/README.md b/2020/Day1/Elixir/README.md new file mode 100644 index 0000000..d1ba727 --- /dev/null +++ b/2020/Day1/Elixir/README.md @@ -0,0 +1,21 @@ +# Day1 + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `day1` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:day1, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at [https://hexdocs.pm/day1](https://hexdocs.pm/day1). + diff --git a/2020/Day1/Elixir/input.txt b/2020/Day1/Elixir/input.txt new file mode 100644 index 0000000..4e18c64 --- /dev/null +++ b/2020/Day1/Elixir/input.txt @@ -0,0 +1,200 @@ +1429 +1368 +1661 +1687 +1593 +1495 +1565 +1500 +1635 +1845 +1645 +1999 +1415 +1054 +1930 +1774 +1405 +1993 +1757 +1623 +1675 +1665 +631 +1950 +1702 +1311 +1509 +1790 +1643 +1884 +226 +1455 +1679 +1746 +1284 +1342 +1684 +1543 +1396 +1806 +1523 +1363 +1011 +1577 +1767 +1287 +1885 +1517 +1556 +1722 +1260 +1624 +1466 +1263 +1162 +1688 +1202 +1913 +1964 +1385 +1970 +1976 +1431 +858 +1748 +1544 +1438 +1300 +1926 +1587 +1376 +1939 +1039 +1639 +1539 +1491 +1631 +1521 +1564 +1507 +1637 +1534 +1713 +1533 +1118 +1356 +2003 +282 +1079 +1837 +1259 +1941 +1836 +1903 +1433 +1467 +1027 +1441 +1048 +1742 +1087 +1872 +1476 +1657 +1361 +1182 +1494 +1529 +1822 +1444 +1330 +1514 +1723 +1432 +1683 +1997 +1443 +1474 +1932 +1504 +1313 +1765 +19 +1784 +1619 +992 +1560 +1680 +1626 +1558 +1899 +1293 +1676 +1161 +1140 +1341 +1597 +1628 +1611 +1302 +1269 +1241 +1952 +1591 +1726 +428 +1703 +1289 +1109 +1478 +1002 +1817 +1849 +1838 +1319 +1641 +583 +1920 +1453 +1411 +1870 +1763 +1469 +1646 +1719 +1213 +1462 +1545 +1682 +1711 +18 +2004 +1252 +1620 +1559 +1315 +781 +1656 +1987 +1436 +1630 +1985 +1897 +1551 +1296 +1282 +1735 +1320 +1659 +1271 +1380 +1274 +1876 +1492 +1298 +1399 +1692 +1265 +1555 +1337 \ No newline at end of file diff --git a/2020/Day1/Elixir/lib/day1.ex b/2020/Day1/Elixir/lib/day1.ex new file mode 100644 index 0000000..5891cc4 --- /dev/null +++ b/2020/Day1/Elixir/lib/day1.ex @@ -0,0 +1,34 @@ +# iex()> Day1.load_file |> Day1.first + +defmodule Day1 do + def load_file() do + File.read!("input.txt") + |> String.split("\n", trim: true) + |> Enum.map(fn n -> Integer.parse(n) end) + |> Enum.map(fn tup -> Kernel.elem(tup, 0) end) + end + + @spec first([Integer]) :: Integer + def first(input) do + Enum.reduce(input, 0, fn curr1, res1 -> + if res1 > 0 and res1 != nil do + res1 + else + Enum.reduce(input, 0, fn curr2, res2 -> + if res2 > 0 and res2 != nil do + res2 + else + if curr1 + curr2 == 2020 do + curr1 * curr2 + end + end + end) + end + end) + end + + @spec second([Integer]) :: Integer + def second(input) do + :second + end +end diff --git a/2020/Day1/Elixir/mix.exs b/2020/Day1/Elixir/mix.exs new file mode 100644 index 0000000..6fb934e --- /dev/null +++ b/2020/Day1/Elixir/mix.exs @@ -0,0 +1,28 @@ +defmodule Day1.MixProject do + use Mix.Project + + def project do + [ + app: :day1, + version: "0.1.0", + elixir: "~> 1.12", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/2020/Day1/Elixir/test/day1_test.exs b/2020/Day1/Elixir/test/day1_test.exs new file mode 100644 index 0000000..a354d96 --- /dev/null +++ b/2020/Day1/Elixir/test/day1_test.exs @@ -0,0 +1,8 @@ +defmodule Day1Test do + use ExUnit.Case + doctest Day1 + + test "greets the world" do + assert Day1.hello() == :world + end +end diff --git a/2020/Day1/Elixir/test/test_helper.exs b/2020/Day1/Elixir/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/2020/Day1/Elixir/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()