diff --git a/day13.erl b/day13.erl index 20589f6..1f1e28a 100644 --- a/day13.erl +++ b/day13.erl @@ -2,12 +2,10 @@ -export([solve/1]). --compile(export_all). - solve(Input) -> PacketPairs = binary:split(Input, <<"\n\n">>, [global, trim_all]), Part1 = part1(PacketPairs), - {Part1, none}. + {Part1, part2(Input)}. part1(PacketPairs) -> part1(PacketPairs, {1, 0}). @@ -22,6 +20,19 @@ part1([PacketPair|Rest], {Index, Score}) -> end, part1(Rest, {Index+1, NewScore}). +part2(Input) -> + Packets0 = binary:split(Input, <<"\n">>, [global, trim_all]), + Packets1 = lists:filter(fun(X) -> byte_size(X) > 0 end, Packets0), + Packets2 = lists:map(fun parse_packet/1, Packets1), + Packets3 = [[[2]], [[6]] | Packets2], % add signal packets + Packets = lists:sort(fun compare/2, Packets3), + find([[2]], Packets) * find([[6]], Packets). + +find(Item, List) -> find(Item, List, 1). +find(_Item, [], _Index) -> 0; +find(Item, [Item | _Rest], Index) -> Index; +find(Item, [_Head | Rest], Index) -> find(Item, Rest, Index+1). + compare([], []) -> undecided; compare([], [_|_Rest]) -> true; compare([_|_Rest], []) -> false;