Browse Source

Day 13-2

trunk
Shanti Chellaram 3 years ago
parent
commit
2a4a438c3d
  1. 17
      day13.erl

17
day13.erl

@ -2,12 +2,10 @@
-export([solve/1]). -export([solve/1]).
-compile(export_all).
solve(Input) -> solve(Input) ->
PacketPairs = binary:split(Input, <<"\n\n">>, [global, trim_all]), PacketPairs = binary:split(Input, <<"\n\n">>, [global, trim_all]),
Part1 = part1(PacketPairs), Part1 = part1(PacketPairs),
{Part1, none}.
{Part1, part2(Input)}.
part1(PacketPairs) -> part1(PacketPairs, {1, 0}). part1(PacketPairs) -> part1(PacketPairs, {1, 0}).
@ -22,6 +20,19 @@ part1([PacketPair|Rest], {Index, Score}) ->
end, end,
part1(Rest, {Index+1, NewScore}). 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([], []) -> undecided;
compare([], [_|_Rest]) -> true; compare([], [_|_Rest]) -> true;
compare([_|_Rest], []) -> false; compare([_|_Rest], []) -> false;

Loading…
Cancel
Save