You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
831 B

-module(day3).
-export([solve/1]).
solve(InputData) ->
Rucksacks = binary:split(InputData, <<$\n>>, [trim_all, global]),
{lists:foldl(fun asdf/2, 0, Rucksacks), none}.
asdf(Rucksack, TotalPriority) ->
CompartmentSize = byte_size(Rucksack) div 2,
<<Compartment1:CompartmentSize/binary, Compartment2:CompartmentSize/binary>> = Rucksack,
[DupeLetter] = maps:keys(sets:intersection(tally(Compartment1), tally(Compartment2))),
priority(DupeLetter) + TotalPriority.
tally(Compartment) -> tally(Compartment, #{}).
tally(<<>>, Set) -> Set;
tally(<<Item, Rest/binary>>, Set) when is_map_key(Item, Set) -> tally(Rest, Set);
tally(<<Item, Rest/binary>>, Set) -> tally(Rest, Set#{Item => []}).
priority(Item) when Item >= $a andalso Item =< $z -> Item - $a + 1;
priority(Item) when Item >= $A andalso Item =< $Z -> Item - $A + 27.