[TASK] Day 2 part 2
This commit is contained in:
@@ -5,26 +5,55 @@ defmodule Day2 do
|
||||
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 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)
|
||||
end |> Enum.sum
|
||||
eval_range(from, to, id_checker)
|
||||
end
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
def eval_range(from, to) do
|
||||
Enum.filter(from..to, fn x -> is_valid_id(x) end) |> Enum.sum
|
||||
def eval_range(from, to, id_checker) do
|
||||
Enum.filter(from..to, fn x -> id_checker.(x) end) |> Enum.sum()
|
||||
end
|
||||
|
||||
def is_valid_id(id) do
|
||||
def is_invalid_id_pt1(id) do
|
||||
digit_count = Enum.count(Integer.digits(id))
|
||||
power = 10 ** (div(digit_count, 2))
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user