1
1
Fork 0
aoc2018/day2.exs

48 lines
1.1 KiB
Elixir
Raw Normal View History

2018-12-02 05:43:29 +00:00
defmodule DayTwo do
def part_one(input) do
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1)
2018-12-03 12:23:00 +00:00
two =
Enum.map(lines, fn string -> DayTwo.check_occurence(string, 2) end)
|> Enum.filter(& &1)
three =
Enum.map(lines, fn string -> DayTwo.check_occurence(string, 3) end)
|> Enum.filter(& &1)
2018-12-02 05:43:29 +00:00
length(two) * length(three)
end
2018-12-03 12:23:00 +00:00
def check_occurence(line, req) do
size =
Enum.reduce(line, %{}, fn x, acc -> Map.update(acc, x, 1, &(&1 + 1)) end)
|> Enum.into([])
|> Enum.filter(fn {_, count} -> count == req end)
|> length
2018-12-02 05:43:29 +00:00
size > 0
end
def part_two(input) do
lines = String.split(input, "\n") |> Enum.map(&String.codepoints/1)
2018-12-03 12:23:00 +00:00
2018-12-02 05:43:29 +00:00
Enum.map(lines, fn line ->
2018-12-03 12:23:00 +00:00
Enum.map(lines, fn linetwo ->
2018-12-02 05:43:29 +00:00
diff = compare_strings(line, linetwo)
2018-12-03 12:23:00 +00:00
2018-12-02 05:43:29 +00:00
if diff == 1 do
IO.puts("#{line} #{linetwo}")
end
end)
end)
2018-12-03 12:23:00 +00:00
2018-12-02 05:43:29 +00:00
nil
end
def compare_strings(one, two) do
Enum.zip(one, two)
2018-12-03 12:23:00 +00:00
|> Enum.filter(fn {one, two} -> one != two end)
|> length
2018-12-02 05:43:29 +00:00
end
end