Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
martindahlstrand committed Apr 4, 2012
2 parents 24c0656 + 69e6e8d commit bc644b9
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 15 deletions.
9 changes: 0 additions & 9 deletions README

This file was deleted.

18 changes: 18 additions & 0 deletions prolog/day_one/coloring.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
adjacent(1,2).
adjacent(1,3).
adjacent(1,4).
adjacent(1,5).
adjacent(2,3).
adjacent(2,4).
adjacent(3,4).
adjacent(4,5).
adjacent(2,1).
adjacent(3,1).
adjacent(4,1).
adjacent(5,1).
adjacent(3,2).
adjacent(4,2).
adjacent(4,3).
adjacent(5,4).

adjacent(2,3).
10 changes: 10 additions & 0 deletions prolog/day_one/food_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
food_type(velveeta, cheese).
food_type(ritz, cracker).
food_type(spam, meat).
food_type(sausage, meat).
food_type(jolt, soda).
food_type(twinkie, dessert).
flavor(sweet, dessert).
flavor(savory, meat).
flavor(savory, cheese).
flavor(sweet, soda).
13 changes: 8 additions & 5 deletions prolog/day_one/je_books.pl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
book(lotr,[tolkien]).
book(hitchikers_guide, [adams]).
book(patterns,[gamma, vlissides, helm, johnson]).
%-- author([]).
%-- author([Head|Tail]) :- book(_,Head), author(Tail).
book(lotr,tolkien).
book(hitchikers_guide, adams).
book(patterns, johnson).
book(patterns, helm).
book(patterns, vlissides).
book(patterns, gamma).

author(Author, Book) :- book(Book,Author).
15 changes: 15 additions & 0 deletions prolog/day_one/map_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
different(red, green). different(red, blue).
different(green, red). different(green, blue).
different(blue, red). different(blue, green).
coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
different(Mississippi, Tennessee),
different(Mississippi, Alabama),
different(Alabama, Tennessee),
different(Alabama, Mississippi),
different(Alabama, Georgia),
different(Alabama, Florida),
different(Georgia, Florida),
different(Georgia, Tennessee).
different(Canada, Tennessee).


33 changes: 33 additions & 0 deletions prolog/day_three/je_queens.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
valid_position((Row,Column)):- member(Row,[1,2,3,4,5,6,7,8]), member(Column,[1,2,3,4,5,6,7,8]).
valid_positions([]).
valid_positions([Queen|Tail]) :- valid_position(Queen),valid_positions(Tail).


collect_rows([],[]).
collect_rows([(Row,_)|Tail],[Row|RTail]) :- collect_rows(Tail, RTail). %-- recurse and stuff values in row
unique_rows(Board) :- collect_rows(Board,Rows), fd_all_different(Rows). %-- collect and check rows


collect_columns([],[]).
collect_columns([(_,Col)|Tail],[Col|CTail]) :- collect_columns(Tail, CTail). %-- recurse and stuff values in row
unique_columns(Board) :- collect_columns(Board,Cols), fd_all_different(Cols). %-- collect and check rows

diagonal_one([],[]).
diagonal_one([(Row,Col)|Tail], [Y|DTail]) :- Y is (Row-Col), diagonal_one(Tail,DTail). %-- subtracting row & Col is inspired (1,1 & 2,2 is on same diagonal (0))

diagonal_two([],[]).
diagonal_two([(Row,Col)|Tail], [Y|DTail]) :- Y is (Row+Col), diagonal_two(Tail,DTail). %-- adding row & Col is inspired (1,2) and (2,1) is on same diagonal(3)

unique_diagonals(Board) :- diagonal_one(Board, DiagonalOne),
fd_all_different(DiagonalOne),
diagonal_two(Board, DiagonalTwo), fd_all_different(DiagonalTwo).



queens(Board) :-
valid_positions(Board),
unique_rows(Board),
unique_columns(Board),
unique_diagonals(Board).


37 changes: 37 additions & 0 deletions prolog/day_three/je_sudoku.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
four_sudoku(Puzzle, Solution) :-
Solution = Puzzle,
Puzzle =
[S11,S12,S13,S14,
S21,S22,S23,S24,
S31,S32,S33,S34,
S41,S42,S43,S44],
fd_domain(Solution,1,4),

Row1=[S11,S12,S13,S14],
Row2=[S21,S22,S23,S24],
Row3=[S31,S32,S33,S34],
Row4=[S41,S42,S43,S44],

Col1=[S11,S21,S31,S41],
Col2=[S12,S22,S32,S42],
Col3=[S13,S23,S33,S43],
Col4=[S14,S24,S34,S44],

Sq1=[S11,S12,S21,S22],
Sq2=[S31,S32,S41,S42],
Sq3=[S13,S14,S23,S24],
Sq4=[S33,S34,S43,S44],

fd_all_different(Row1),
fd_all_different(Row2),
fd_all_different(Row3),
fd_all_different(Row4),
fd_all_different(Col1),
fd_all_different(Col2),
fd_all_different(Col3),
fd_all_different(Col4),
fd_all_different(Sq1),
fd_all_different(Sq2),
fd_all_different(Sq3),
fd_all_different(Sq4).

1 change: 1 addition & 0 deletions prolog/day_three/sudoku_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions prolog/day_two/concat_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

concatenate([], List, List).
concatenate([Head|Tail1], List, [Head|Tail2]) :-
concatenate(Tail1, List, Tail2)
4 changes: 4 additions & 0 deletions prolog/day_two/fib_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fib(0,0).
fib(1,1).
fib(N,FIB):-
N>1,N1 is N-1, N2 is N -2, fib((N1),X) , fib(N2,Y), FIB is X+Y.
3 changes: 3 additions & 0 deletions prolog/day_two/je_fib.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fib(0,0).
fib(1,1).
fib(N, Result):- First is Nth -1, Second is Nth-2, fib(First,FirstResult), fib(Second, SecondResult), Result is (FirstResult + SecondResult).
2 changes: 1 addition & 1 deletion prolog/day_two/je_list_reverse.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
rev([], Rev, Rev).
rev([Head|Tail],Temp,Rev) :- rev(Tail,[Head|Temp], Rev).

rev(List,Target) :- rev(List,[],Target).
%-- vill göra såhär...
rev2([],[]).
rev2([Head|XTail],[RevTail|Head]) :- rev2(XTail, RevTail).
Expand Down
4 changes: 4 additions & 0 deletions prolog/day_two/last_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
last([],[]).
last([Head|[TailHead|TailTail]],Last):-
last(TailTail,Last),TailHead.

5 changes: 5 additions & 0 deletions prolog/day_two/list_math_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
count(0, []).
count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1.
sum(0, []).
sum(Total, [Head|Tail]) :- sum(Sum, Tail), Total is Head + Sum.
average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum/Count.
12 changes: 12 additions & 0 deletions prolog/day_two/reverse_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

reverseA([],List, List).
reverseA([Head|Tail],Temp,Ny):-
reverseA(Tail,[Head, Temp],Ny).

rev([], Rev, Rev).
rev([Head|Tail],Temp,Rev) :- rev(Tail,[Head|Temp], Rev).


reverseB([],[]).
reverseB([Head|Tail],[List,Head]) :-
reverseB(Tail,List).
15 changes: 15 additions & 0 deletions prolog/day_two/sort_ma.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sortera([],[]).
sortera([A],[A]).
sortera([A,B|TAIL],AB):-
newList(A,B,C),sortera(TAIL,C)

newList(A,B,C):-
( A =< B
-> C = [C, A,B]
; C = [C, B,A]
).
newList(A,B,[]):-
( A =< B
-> C = [A,B]
; C = [B,A]
).

0 comments on commit bc644b9

Please sign in to comment.