[TASK] Day 2, part 1 in Elixir

This commit is contained in:
2025-12-07 23:34:59 +01:00
parent 5f4869705c
commit 385c5c4a30
9 changed files with 99 additions and 1 deletions

View File

@@ -1,3 +1,3 @@
# advent-of-code-2025
My solutions to advent of code 2025
My solutions to advent of code 2025. This year I decided to do a different language each day.

View File

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

23
day2-elixir/.gitignore vendored Normal file
View File

@@ -0,0 +1,23 @@
# 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/
# 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").
day2-*.tar
# Temporary files, for example, from tests.
/tmp/

View File

@@ -0,0 +1 @@
749639-858415,65630137-65704528,10662-29791,1-17,9897536-10087630,1239-2285,1380136-1595466,8238934-8372812,211440-256482,623-1205,102561-122442,91871983-91968838,62364163-62554867,3737324037-3737408513,9494926669-9494965937,9939271919-9939349036,83764103-83929201,24784655-24849904,166-605,991665-1015125,262373-399735,557161-618450,937905586-937994967,71647091-71771804,8882706-9059390,2546-10476,4955694516-4955781763,47437-99032,645402-707561,27-86,97-157,894084-989884,421072-462151

View File

@@ -0,0 +1 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124

30
day2-elixir/lib/day2.ex Normal file
View File

@@ -0,0 +1,30 @@
defmodule Day2 do
@moduledoc """
Documentation for `Day2`.
"""
require Integer
def start(_type, _args) do
input_file = File.read("input/day2.txt")
IO.puts "Part 1: #{Day2.part1(elem(input_file, 1))}"
end
def part1(input) do
for range <- String.split(input, ",") do
[from, to] = String.split(range, "-") |> Enum.map(fn s -> String.to_integer(s) end)
eval_range(from, to)
end |> Enum.sum
end
def eval_range(from, to) do
Enum.filter(from..to, fn x -> is_valid_id(x) end) |> Enum.sum
end
def is_valid_id(id) do
digit_count = Enum.count(Integer.digits(id))
power = 10 ** (div(digit_count, 2))
# IO.inspect {id, digit_count, power, div(id, power), rem(id, power)}
Integer.is_even(digit_count) && div(id, power) == rem(id, power)
end
end

29
day2-elixir/mix.exs Normal file
View File

@@ -0,0 +1,29 @@
defmodule Day2.MixProject do
use Mix.Project
def project do
[
app: :day2,
version: "0.1.0",
elixir: "~> 1.18",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
mod: { Day2, []},
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

View File

@@ -0,0 +1,9 @@
defmodule Day2Test do
use ExUnit.Case
doctest Day2
test "Part 1 example input" do
input_file = File.read("input/day2_example.txt")
assert Day2.part1(elem(input_file, 1)) == 1227775554
end
end

View File

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