Advertisement
FubFubFub

Day 6

Dec 7th, 2022
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.78 KB | Source Code | 0 0
  1. % This is true if the four-character sequence in Signal starting at MarkerPos could be a marker. Take the segment, turn it into a list, make it a set (removing duplicates), check that the length is 4.
  2. marker_at(Signal, MarkerPos) :- sub_atom(Signal, MarkerPos, 4, _, Segment), atom_chars(Segment, CharList), setof(X, member(X, CharList), UniqueList), length(UniqueList, 4).
  3.  
  4. % Solve the puzzle. Iterate over the length of the signal, check if there could be a marker on that position. Then take the first item in the list because that is the first marker.
  5. solve(Signal, Counter) :- atom_length(Signal, SignalSize), MaxMarkerPos is SignalSize - 5, findall(X, (between(1, MaxMarkerPos, MarkerPos), marker_at(Signal, MarkerPos), X is MarkerPos + 4), CounterList), nth(1, CounterList, Counter).
  6.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement