aboutsummaryrefslogtreecommitdiff
path: root/challenge-098
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2021-02-07 14:16:34 -0500
committerAdam Russell <ac.russell@live.com>2021-02-07 14:16:34 -0500
commit3936f9fecbe381c3f634ee2dc7ff3f79100bbd81 (patch)
tree19710eff372d6d929ff8b5a418f121d6cc880eb2 /challenge-098
parentd62afdede27d500ef383c7466edd92ffa909adca (diff)
downloadperlweeklychallenge-club-3936f9fecbe381c3f634ee2dc7ff3f79100bbd81.tar.gz
perlweeklychallenge-club-3936f9fecbe381c3f634ee2dc7ff3f79100bbd81.tar.bz2
perlweeklychallenge-club-3936f9fecbe381c3f634ee2dc7ff3f79100bbd81.zip
Prolog solutions for challenge 098.
Diffstat (limited to 'challenge-098')
-rw-r--r--challenge-098/adam-russell/blog1.txt1
-rw-r--r--challenge-098/adam-russell/prolog/ch-1.p37
-rw-r--r--challenge-098/adam-russell/prolog/ch-2.p38
3 files changed, 76 insertions, 0 deletions
diff --git a/challenge-098/adam-russell/blog1.txt b/challenge-098/adam-russell/blog1.txt
new file mode 100644
index 0000000000..816788ff86
--- /dev/null
+++ b/challenge-098/adam-russell/blog1.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/02/07
diff --git a/challenge-098/adam-russell/prolog/ch-1.p b/challenge-098/adam-russell/prolog/ch-1.p
new file mode 100644
index 0000000000..7a6757830a
--- /dev/null
+++ b/challenge-098/adam-russell/prolog/ch-1.p
@@ -0,0 +1,37 @@
+:-dynamic(position/1).
+:-initialization(main).
+
+position(0).
+
+read_n_chars(Stream, N, Chars):-
+ read_n_chars(Stream, N, [], Chars).
+read_n_chars(_, 0, ByteAccum, Chars):-
+ atom_codes(Chars, ByteAccum).
+read_n_chars(Stream, N, ByteAccum, Chars):-
+ \+ at_end_of_stream(Stream),
+ get_byte(Stream, C),
+ N0 is N - 1,
+ append(ByteAccum, [C], ByteAccum0),
+ read_n_chars(Stream, N0, ByteAccum0, Chars).
+read_n_chars(Stream, _, ByteAccum, Chars):-
+ at_end_of_stream(Stream),
+ read_n_chars(Stream, 0, ByteAccum, Chars).
+
+read_n(File, N, Chars):-
+ open(File, read, Stream,[type(binary),reposition(true)]),
+ position(Position),
+ seek(Stream, bof, Position, _),
+ read_n_chars(Stream, N, Chars),
+ X is N + Position,
+ retract(position(Position)),
+ asserta(position(X)),
+ close(Stream).
+
+main:-
+ read_n('../ch-1.dat', 4, Chars0),
+ write(Chars0), nl,
+ read_n('../ch-1.dat', 4, Chars1),
+ write(Chars1), nl,
+ read_n('../ch-1.dat', 4, Chars2),
+ write(Chars2), nl,
+ halt. \ No newline at end of file
diff --git a/challenge-098/adam-russell/prolog/ch-2.p b/challenge-098/adam-russell/prolog/ch-2.p
new file mode 100644
index 0000000000..c31d54817f
--- /dev/null
+++ b/challenge-098/adam-russell/prolog/ch-2.p
@@ -0,0 +1,38 @@
+:-initialization(main).
+
+needle_haystack([H|T], N, Index):-
+ ((N < H, Index is 0);
+ (last(T, Last), N > Last,
+ length([H|T], Length), Index is Length)).
+needle_haystack([H|T], N, Index):-
+ needle_haystack([H|T], N, 0, Index).
+needle_haystack([], _, Index, Index).
+needle_haystack([_|[]], _, Index, Index).
+needle_haystack([H0, _|_], N, Counter, Index):-
+ H0 == N,
+ Index is Counter.
+needle_haystack([_, H1|_], N, Counter, Index):-
+ H1 == N,
+ Index is Counter + 1.
+needle_haystack([H0, H1|T], N, Counter, Index):-
+ H0 \== N,
+ H1 \== N,
+ \+ between(H0, H1, N),
+ C is Counter + 2,
+ needle_haystack(T, N, C, Index).
+needle_haystack([H0, H1|_], N, Counter, Index):-
+ H0 \== N,
+ H1 \== N,
+ between(H0, H1, N),
+ Index is Counter + 1.
+
+main:-
+ needle_haystack([1, 2, 3, 4], 3, Index0),
+ write(Index0), nl,
+ needle_haystack([1, 3, 5, 7], 6, Index1),
+ write(Index1), nl,
+ needle_haystack([12, 14, 16, 18], 10, Index2),
+ write(Index2), nl,
+ needle_haystack([11, 13, 15, 17], 19, Index3),
+ write(Index3), nl,
+ halt. \ No newline at end of file