31 lines
808 B
Elixir
31 lines
808 B
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))}"
|
|
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
|