diff options
| author | Adam Russell <ac.russell@live.com> | 2020-11-29 16:26:21 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2020-11-29 16:26:21 -0500 |
| commit | 0fb1e10f0b233caf4b8b46fd732f9d1d30b79fec (patch) | |
| tree | 42db389e068037b72f07a9110cc48faa4f27aa08 /challenge-088 | |
| parent | 42de3429122f018e501f84453cd40983e0541c98 (diff) | |
| download | perlweeklychallenge-club-0fb1e10f0b233caf4b8b46fd732f9d1d30b79fec.tar.gz perlweeklychallenge-club-0fb1e10f0b233caf4b8b46fd732f9d1d30b79fec.tar.bz2 perlweeklychallenge-club-0fb1e10f0b233caf4b8b46fd732f9d1d30b79fec.zip | |
Prolog solution to part 2.
Diffstat (limited to 'challenge-088')
| -rw-r--r-- | challenge-088/adam-russell/prolog/ch-2.p | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-088/adam-russell/prolog/ch-2.p b/challenge-088/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..7c780ff4ae --- /dev/null +++ b/challenge-088/adam-russell/prolog/ch-2.p @@ -0,0 +1,65 @@ +/* + You are given m x n matrix of positive integers. + Write a script to print spiral matrix as a list. +*/ +write_remove_top(Matrix, UpdatedMatrix):- + nth0(0, Matrix, Top), + atomic_list_concat(Top, ",", TopString), + write(TopString), + nth0(0, Matrix, _, UpdatedMatrix). + +write_remove_last([], UpdatedMatrix, UpdatedMatrix). +write_remove_last([H|T], RemainderAccum, UpdatedMatrix):- + length(H, L), + Last is L - 1, + nth0(Last, H, Right), + write(Right), + write(","), + nth0(Last, H, _, UpdatedRow), + write_remove_last(T, [UpdatedRow|RemainderAccum], UpdatedMatrix). + +write_remove_right(Matrix, UpdatedMatrix):- + write_remove_last(Matrix, [], UpdatedMatrix). + +write_remove_first([], UpdatedMatrix, UpdatedMatrix). +write_remove_first([H|T], RemainderAccum, UpdatedMatrix):- + nth0(0, H, Left), + write(Left), + write(","), + nth0(0, H, _, UpdatedRow), + write_remove_first(T, [UpdatedRow|RemainderAccum], UpdatedMatrix). + +write_remove_left(Matrix, UpdatedMatrix):- + write_remove_first(Matrix, [], UpdatedMatrix). + +write_remove_bottom(Matrix, UpdatedMatrix):- + length(Matrix, L), + Last is L - 1, + nth0(Last, Matrix, Bottom), + reverse(Bottom, ReverseBottom), + atomic_list_concat(ReverseBottom, ",", BottomString), + write(BottomString), + nth0(Last, Matrix, _, UpdatedMatrix). + + +spiral(Matrix):- + spiral(Matrix, _). +spiral([], _). +spiral(Matrix, UpdatedMatrix):- + write_remove_top(Matrix, UpdatedMatrix), + write(","), + write_remove_right(UpdatedMatrix, RightRemainder), + reverse(RightRemainder, RemainderRight), + write_remove_bottom(RemainderRight, BottomRemainder), + write(","), + write_remove_left(BottomRemainder, LeftRemainder), + spiral(LeftRemainder, []). +spiral(_, []):- + write("\b"). + +main:- + spiral([ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] + ]), halt. |
