diff --git a/day6.erl b/day6.erl index 4e61fc6..4e13031 100644 --- a/day6.erl +++ b/day6.erl @@ -3,13 +3,12 @@ -export([solve/1]). solve(InputData) -> - {solve(InputData, 1), none}. - -solve(<>, N) -> solve(<>, N+1); -solve(<>, N) -> solve(<>, N+1); -solve(<>, N) -> solve(<>, N+1); -solve(<>, N) -> solve(<>, N+2); -solve(<>, N) -> solve(<>, N+3); -solve(<>, N) when A /= B andalso B /= C andalso C /= D andalso - A /= C andalso B /= D andalso A /= D -> N+3; -solve(<<_, Rest/binary>>, N) -> solve(Rest, N+1). + {solve2(InputData, 4), solve2(InputData, 14)}. + +solve2(Symbols, Target) -> solve2(Symbols, #{}, 0, 0, Target). + +solve2(_Symbols, _State, Target, N, Target) -> N; + +solve2(<>, LastSeen, ChainLength, N, Target) -> + Last = maps:get(Symbol, LastSeen, 0), + solve2(Symbols, LastSeen#{Symbol => N}, min(N-Last, ChainLength+1), N+1, Target).