Day 4: Camp Cleanup
Mix.install([{:kino, "~> 0.7.0"}])
Day 4
sample_input = Kino.Input.textarea("Paste Sample Input")
real_input = Kino.Input.textarea("Paste Real Input")
parse_single_assignment = fn spec ->
spec
|> String.split("-")
|> Enum.map(&String.to_integer/1)
|> List.to_tuple()
end
parse_assignments = fn input ->
input
|> Kino.Input.read()
|> String.split()
|> Enum.map(fn line ->
line
|> String.split(",")
|> Enum.map(parse_single_assignment)
|> List.to_tuple()
end)
end
contains? = fn {start_one, end_one}, {start_two, end_two} ->
start_one <= start_two and end_one >= end_two
end
subset? = fn
{one, two} -> contains?.(one, two) or contains?.(two, one)
end
overlap? = fn {{start_one, end_one}, {start_two, end_two}} ->
!(end_one < start_two or start_one > end_two)
end
count_subsets = &Enum.count(&1, subset?)
count_overlap = &Enum.count(&1, overlap?)
sample_input |> parse_assignments.() |> count_subsets.()
real_input |> parse_assignments.() |> count_subsets.()
sample_input |> parse_assignments.() |> count_overlap.()
real_input |> parse_assignments.() |> count_overlap.()