[TASK] Day 2 part 2
This commit is contained in:
@@ -1 +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
|
||||
853-1994,1919078809-1919280414,1212082623-1212155811,2389-4173,863031-957102,9393261874-9393318257,541406-571080,1207634-1357714,36706-61095,6969667126-6969740758,761827-786237,5516637-5602471,211490-235924,282259781-282327082,587606-694322,960371-1022108,246136-353607,3-20,99-182,166156087-166181497,422-815,82805006-82876926,14165-30447,4775-7265,83298136-83428425,2439997-2463364,44-89,435793-511395,3291059-3440895,77768624-77786844,186-295,62668-105646,7490-11616,23-41,22951285-23017127
|
||||
@@ -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
|
||||
|
||||
@@ -4,6 +4,11 @@ defmodule Day2Test do
|
||||
|
||||
test "Part 1 example input" do
|
||||
input_file = File.read("input/day2_example.txt")
|
||||
assert Day2.part1(elem(input_file, 1)) == 1227775554
|
||||
assert Day2.part1(elem(input_file, 1)) == 1_227_775_554
|
||||
end
|
||||
|
||||
test "Part 2 example input" do
|
||||
input_file = File.read("input/day2_example.txt")
|
||||
assert Day2.part2(elem(input_file, 1)) == 4_174_379_265
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user