-module(day4). -export([solve/1]). solve(InputData) -> Lines = binary:split(InputData, <<$\n>>, [trim_all, global]), solver(Lines, {0, 0}). solver([], Acc) -> Acc; solver([Entry | Rest], {Part1, Part2}) -> Ranges = binary:split(Entry, <<",">>, [trim_all, global]), [[F1, F2], [S1, S2]] = lists:map(fun(Range) -> binary:split(Range, <<"-">>, [trim_all, global]) end, Ranges), First = {binary_to_integer(F1), binary_to_integer(F2)}, Second = {binary_to_integer(S1), binary_to_integer(S2)}, Intersection = range_intersect(First, Second), {P1Flag, P2Flag} = case Intersection of First -> {1, 1}; Second -> {1, 1}; SomethingElse -> case range_is_empty(Intersection) of false -> {0, 1}; true -> {0, 0} end end, solver(Rest, {Part1+P1Flag, Part2+P2Flag}). range_is_empty({Bottom, Top}) -> Top < Bottom. range_intersect({B1, T1}, {B2, T2}) -> {max(B1, B2), min(T1, T2)}.