Skip to content

Commit 98390fd

Browse files
2022 day 01 in Elixir
1 parent ba6dcd8 commit 98390fd

File tree

10 files changed

+2417
-38
lines changed

10 files changed

+2417
-38
lines changed

2022/elixir/.formatter.exs

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

2022/elixir/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
advent_of_code_2022-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/

2022/elixir/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# AdventOfCode2022
2+
3+
Solve exercises using [lib/mix/tasks/solve.ex](./lib/mix/tasks/solve.ex):
4+
5+
```sh
6+
mix solve day01
7+
```

2022/elixir/lib/day01.ex

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Day01 do
2+
@input File.read!("../input/day01.txt")
3+
4+
def part_one(input \\ @input) do
5+
parse_input(input)
6+
|> Enum.max()
7+
end
8+
9+
def part_two(input \\ @input) do
10+
parse_input(input)
11+
|> Enum.sort(:desc)
12+
|> Enum.take(3)
13+
|> Enum.sum()
14+
end
15+
16+
defp parse_input(input) do
17+
input
18+
|> String.trim()
19+
|> String.split("\n\n")
20+
|> Enum.map(fn block ->
21+
block
22+
|> String.split("\n")
23+
|> Enum.map(&String.to_integer/1)
24+
|> Enum.sum()
25+
end)
26+
end
27+
end

2022/elixir/lib/mix/tasks/solve.ex

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Mix.Tasks.Solve do
2+
use Mix.Task
3+
4+
def run([module | _]) do
5+
apply(String.to_existing_atom("Elixir.#{String.capitalize(module)}"), :part_one, [])
6+
|> IO.inspect(label: "Part one")
7+
8+
apply(String.to_existing_atom("Elixir.#{String.capitalize(module)}"), :part_two, [])
9+
|> IO.inspect(label: "Part two")
10+
end
11+
end

2022/elixir/mix.exs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule AdventOfCode2022.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :advent_of_code_2022,
7+
version: "0.1.0",
8+
elixir: "~> 1.15",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

2022/elixir/test/day01_test.exs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule Day01Test do
2+
use ExUnit.Case
3+
4+
@example_input """
5+
1000
6+
2000
7+
3000
8+
9+
4000
10+
11+
5000
12+
6000
13+
14+
7000
15+
8000
16+
9000
17+
18+
10000
19+
"""
20+
21+
test "part one", do: assert(Day01.part_one(@example_input) == 24000)
22+
test "part two", do: assert(Day01.part_two(@example_input) == 45000)
23+
end

2022/elixir/test/test_helper.exs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ExUnit.start()
2+
ExUnit.configure(trace: true)

0 commit comments

Comments
 (0)