advent2022

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

commit ceba7e7870ce19112262f75caae9ae9437a8c39f
parent 66665329bede75b8ab7bd11fed7b6b99b52588c2
Author: Decay <decay@todayiwilllaunchmyinfantsonintoorbit.com>
Date:   Mon,  5 Dec 2022 23:51:21 -0800

Day six in Prolog

Runs like dogshit but works. Takes around 30 seconds to yield an answer
to the second puzzle on a 2018 laptop.

Diffstat:
A6/six.pl | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/6/six.pl b/6/six.pl @@ -0,0 +1,33 @@ +puzzle_data(File, Chars) :- + setup_call_cleanup(open(File, read, In), + stream_input_chars(In, Chars), + close(In)). +stream_input_chars(In, Chars) :- + read_string(In, _, Str), + string_chars(Str, Chars). + +nFromHead(_, 0, _, []). +nFromHead([H|_], 1, _, [H]). +nFromHead([H|T], N, MaxN, [H|RT]) :- nFromHead(T, X, MaxN, RT), + N is X + 1, + MaxN >= N. +nFromHead([H|T], N, [H|RT]) :- nFromHead(T, N, N, RT). + +allUnique([]). +allUnique([_]). +allUnique([H|T]) :- \+(member(H, T)), + allUnique(T). + +fourUnequal([H|Rest], 4) :- nFromHead([H|Rest], 4, FirstFour), + allUnique(FirstFour). +fourUnequal([_|Rest], Pos) :- fourUnequal(Rest, X), Pos is X + 1. + +fourteenUnequal([H|Rest], 14) :- nFromHead([H|Rest], 14, FirstFourteen), + allUnique(FirstFourteen). +fourteenUnequal([_|Rest], Pos) :- fourteenUnequal(Rest, X), Pos is X + 1. + +firstStar(N) :- puzzle_data("input", Chars), + fourUnequal(Chars, N). + +secondStar(N) :- puzzle_data("input", Chars), + fourteenUnequal(Chars, N).