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).
This commit is contained in:
Caleb Webber 2023-10-27 15:47:46 -04:00
parent fc3afda4eb
commit c2933897db
9 changed files with 98 additions and 7 deletions

4
.formatter.exs Normal file
View file

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
.gitignore vendored Normal file
View file

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

21
README.md Normal file
View file

@ -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 <https://hexdocs.pm/aoc>.

View file

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

View file

@ -1,3 +1 @@
import AOCRunner
import Exercise1
AOCRunner.run(Exercise1)
AOCRunner.run(Day1)

28
mix.exs Normal file
View file

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

8
test/aoc_test.exs Normal file
View file

@ -0,0 +1,8 @@
defmodule AocTest do
use ExUnit.Case
doctest Aoc
test "greets the world" do
assert Aoc.hello() == :world
end
end

1
test/test_helper.exs Normal file
View file

@ -0,0 +1 @@
ExUnit.start()