From 1b9d01ccb43ed8767086624ad7d4e6dadaf22145 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Wed, 7 Dec 2022 00:04:23 +0900 Subject: [PATCH] Day 6-2 --- day6.erl | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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).