advent2022

Advent of Code 2022 Solutions
git clone https://todayiwilllaunchmyinfantsonintoorbit.com/advent2022.git
Log | Files | Refs

six.pl (1191B)


      1 puzzle_data(File, Chars) :-
      2     setup_call_cleanup(open(File, read, In),
      3                        stream_input_chars(In, Chars),
      4                        close(In)).
      5 stream_input_chars(In, Chars) :-
      6     read_string(In, _, Str),
      7     string_chars(Str, Chars).
      8 
      9 nFromHead(_, 0, _, []).
     10 nFromHead([H|_], 1, _, [H]).
     11 nFromHead([H|T], N, MaxN, [H|RT]) :- nFromHead(T, X, MaxN, RT),
     12                                      N is X + 1,
     13                                      MaxN >= N.
     14 nFromHead([H|T], N, [H|RT]) :- nFromHead(T, N, N, RT).
     15 
     16 allUnique([]).
     17 allUnique([_]).
     18 allUnique([H|T]) :- \+(member(H, T)),
     19                     allUnique(T).
     20 
     21 fourUnequal([H|Rest], 4) :- nFromHead([H|Rest], 4, FirstFour),
     22                             allUnique(FirstFour).
     23 fourUnequal([_|Rest], Pos) :- fourUnequal(Rest, X), Pos is X + 1.
     24 
     25 fourteenUnequal([H|Rest], 14) :- nFromHead([H|Rest], 14, FirstFourteen),
     26                                  allUnique(FirstFourteen).
     27 fourteenUnequal([_|Rest], Pos) :- fourteenUnequal(Rest, X), Pos is X + 1.
     28 
     29 firstStar(N) :- puzzle_data("input", Chars),
     30                 fourUnequal(Chars, N).
     31 
     32 secondStar(N) :- puzzle_data("input", Chars),
     33                  fourteenUnequal(Chars, N).