This commit is contained in:
zongor 2024-12-02 23:08:24 -05:00
commit 589554e5ed
11 changed files with 2154 additions and 0 deletions

1
README.md Normal file
View File

@ -0,0 +1 @@
# Advent of Code 2024

4
elixir/.formatter.exs Normal file
View File

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

26
elixir/.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").
advent_of_code_2024-*.tar
# Temporary files, for example, from tests.
/tmp/

21
elixir/README.md Normal file
View File

@ -0,0 +1,21 @@
# AdventOfCode2024
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `advent_of_code_2024` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:advent_of_code_2024, "~> 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/advent_of_code_2024>.

36
elixir/lib/day1.ex Normal file
View File

@ -0,0 +1,36 @@
defmodule Day1 do
def part1(file_path) do
file_path
|> File.read!()
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.filter(fn list -> length(list) == 2 end)
|> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
|> Enum.unzip()
|> Tuple.to_list()
|> Enum.map(&Enum.sort/1)
|> Enum.zip()
|> Enum.map(fn {a, b} -> abs(a - b) end)
|> Enum.sum()
end
def part2(file_path) do
file_path
|> File.read!()
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.filter(fn list -> length(list) == 2 end)
|> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
|> Enum.unzip()
|> Tuple.to_list()
|> Enum.map(&Enum.sort/1)
|> (fn [list1, list2] -> {list1, Enum.frequencies(list2)} end).()
|> (fn {list1, freqs} ->
list1
|> Enum.map(&{&1, Map.get(freqs, &1, 0)})
|> Enum.filter(fn {_, count} -> count != 0 end)
|> Enum.map(fn {value, count} -> value * count end)
end).()
|> Enum.sum()
end
end

15
elixir/lib/day2.ex Normal file
View File

@ -0,0 +1,15 @@
defmodule Day2 do
def part1(file_path) do
file_path
|> File.read!()
|> String.split("\n")
|> Enum.map(&String.split/1)
end
def part2(file_path) do
file_path
|> File.read!()
|> String.split("\n")
|> Enum.map(&String.split/1)
end
end

28
elixir/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule AdventOfCode2024.MixProject do
use Mix.Project
def project do
[
app: :advent_of_code_2024,
version: "0.1.0",
elixir: "~> 1.17",
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

1000
elixir/test/1/input Normal file

File diff suppressed because it is too large Load Diff

1000
elixir/test/2/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,22 @@
defmodule AdventOfCode2024Test do
use ExUnit.Case
doctest Day1
test "answers question 1.1" do
Day1.part1("test/1/input")
end
test "answers question 1.2" do
Day1.part2("test/1/input")
end
doctest Day2
test "answers question 2.1" do
Day2.part1("test/1/input")
end
test "answers question 2.2" do
Day2.part2("test/1/input")
end
end

View File

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