Advent of Code (2016) : Day 6
-- Problem --
You can find Part 1 of the problem statement
here. Part 2 is unlocked on successful completion of Part 1.
-- Solution in Elixir --
defmodule Aoc.Day6 do
def parse_input() do
{:ok, input} = File.read("day6_input.txt")
String.split(input)
|> Stream.map(&String.graphemes/1)
|> Enum.reduce(List.duplicate([],8), fn([e1,e2,e3,e4,e5,e6,e7,e8],[l1,l2,l3,l4,l5,l6,l7,l8]) ->
[[e1|l1],[e2|l2],[e3|l3],[e4|l4],[e5|l5],[e6|l6],[e7|l7],[e8|l8]]
end)
end
def most_common_character(list_with_dups) do
Stream.uniq(list_with_dups)
|> Stream.map(&({count_occurrences(list_with_dups, &1), &1}))
|> Enum.max |> elem(1)
end
def least_common_character(list_with_dups) do
Stream.uniq(list_with_dups)
|> Stream.map(&({count_occurrences(list_with_dups, &1), &1}))
|> Enum.min |> elem(1)
end
def count_occurrences(list, target_elem) do
Enum.reduce(list, 0, fn(elem, count) ->
case elem == target_elem do
true -> count + 1
false -> count
end
end)
end
def output_1() do
parse_input()
|> Enum.map_join(&most_common_character/1)
end
def output_2() do
parse_input()
|> Enum.map_join(&least_common_character/1)
end
end
You can find all my Advent of Code (2016) solutions here.