From c2933897db9379229e890810aa1afdcb8668ea1b Mon Sep 17 00:00:00 2001 From: Caleb Webber Date: Fri, 27 Oct 2023 15:47:46 -0400 Subject: [PATCH] switch to project format Fixed a bug in the day1 solution where the list of totals could add the "current sum" for a given elf to the list each time a new entry was added e.g. an input like, 100 200 300 could result in a list like, [300, 200, 100]. Now we reorder the list at each "reset" (empty line). --- .formatter.exs | 4 ++++ .gitignore | 26 ++++++++++++++++++++++++++ README.md | 21 +++++++++++++++++++++ aoc_runner.ex => lib/aoc_runner.ex | 0 day1.ex => lib/day1.ex | 13 +++++++++---- main.exs | 4 +--- mix.exs | 28 ++++++++++++++++++++++++++++ test/aoc_test.exs | 8 ++++++++ test/test_helper.exs | 1 + 9 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 .formatter.exs create mode 100644 .gitignore create mode 100644 README.md rename aoc_runner.ex => lib/aoc_runner.ex (100%) rename day1.ex => lib/day1.ex (81%) create mode 100644 mix.exs create mode 100644 test/aoc_test.exs create mode 100644 test/test_helper.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e23ab19 --- /dev/null +++ b/.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"). +aoc-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..7069072 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Aoc + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `aoc` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:aoc, "~> 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 . + diff --git a/aoc_runner.ex b/lib/aoc_runner.ex similarity index 100% rename from aoc_runner.ex rename to lib/aoc_runner.ex diff --git a/day1.ex b/lib/day1.ex similarity index 81% rename from day1.ex rename to lib/day1.ex index a307198..fbddc3f 100644 --- a/day1.ex +++ b/lib/day1.ex @@ -1,4 +1,4 @@ -defmodule Exercise1 do +defmodule Day1 do def to_command(s) do try do {:add, s |> String.trim() |> String.to_integer()} @@ -26,17 +26,22 @@ defmodule Exercise1 do defp apply_add(n, state) do {current, total, exit} = state new_current = if is_nil(current) do 0 else current end + n - new_total = [ new_current | total ] |> + + {new_current, total, exit} + end + + defp take_top_three(n, list) do + [ n | list ] |> Enum.sort(:desc) |> Enum.take(3) - {new_current, new_total, exit} end defp apply_reset(state) do {current, total, exit} = state + case current do nil -> {current, total, true} - _ -> {nil, total, exit} + _ -> {nil, take_top_three(current, total), exit} end end end diff --git a/main.exs b/main.exs index 1684bb8..3c173fd 100644 --- a/main.exs +++ b/main.exs @@ -1,3 +1 @@ -import AOCRunner -import Exercise1 -AOCRunner.run(Exercise1) +AOCRunner.run(Day1) diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..9c76235 --- /dev/null +++ b/mix.exs @@ -0,0 +1,28 @@ +defmodule Aoc.MixProject do + use Mix.Project + + def project do + [ + app: :aoc, + version: "0.1.0", + elixir: "~> 1.15", + 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/test/aoc_test.exs b/test/aoc_test.exs new file mode 100644 index 0000000..a426096 --- /dev/null +++ b/test/aoc_test.exs @@ -0,0 +1,8 @@ +defmodule AocTest do + use ExUnit.Case + doctest Aoc + + test "greets the world" do + assert Aoc.hello() == :world + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()