Files
advent-of-code-2025/day2-elixir/lib/day2.ex
2025-12-08 00:23:37 +01:00

60 lines
1.5 KiB
Elixir

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))}")
IO.puts("Part 2: #{Day2.part2(elem(input_file, 1))}")
opts = [strategy: :one_for_one, name: A.Supervisor]
Supervisor.start_link([], opts)
end
def part1(input) do
eval(input, &Day2.is_invalid_id_pt1/1)
end
def part2(input) do
eval(input, &Day2.is_invalid_id_pt2/1)
end
def eval(input, id_checker) 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, id_checker)
end
|> Enum.sum()
end
def eval_range(from, to, id_checker) do
Enum.filter(from..to, fn x -> id_checker.(x) end) |> Enum.sum()
end
def is_invalid_id_pt1(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
def is_invalid_id_pt2(id) do
if id <= 10 do
false
else
id_digits = Integer.digits(id)
digit_count = Enum.count(id_digits)
res =
Enum.any?(1..div(digit_count, 2), fn pattern_size ->
sections = Enum.chunk_every(id_digits, pattern_size)
Enum.all?(Enum.drop(sections, 1), fn x -> x == Enum.at(sections, 0) end)
end)
res
end
end
end