day 1
This commit is contained in:
		
						commit
						589554e5ed
					
				| 
						 | 
					@ -0,0 +1,4 @@
 | 
				
			||||||
 | 
					# Used by "mix format"
 | 
				
			||||||
 | 
					[
 | 
				
			||||||
 | 
					  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
| 
						 | 
					@ -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/
 | 
				
			||||||
| 
						 | 
					@ -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>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					ExUnit.start()
 | 
				
			||||||
		Loading…
	
		Reference in New Issue