Skip to content

Instantly share code, notes, and snippets.

View AhmadElsagheer's full-sized avatar

Ahmad Elsagheer AhmadElsagheer

View GitHub Profile
@AhmadElsagheer
AhmadElsagheer / Main.java
Created March 12, 2016 19:07
Problem ACM
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
@AhmadElsagheer
AhmadElsagheer / SendMoreMoney.java
Created December 29, 2017 09:24
SEND MORE MONEY puzzle (CLPFD and Java)
import java.io.IOException;
import java.util.Scanner;
// This codes solves the puzzle WORD1 + WORD2 = WORD3
// The simpler version (SEND MORE MONEY) will require less code.
public class SendMoreMoney {
public static void main(String[] args) throws IOException {
@AhmadElsagheer
AhmadElsagheer / increase.pl
Created December 29, 2017 09:37
Strictly increasing sequence generator
:-use_module(library(clpfd)).
increase(L):-
L ins 1..100,
check_increasing(L),
label(L).
check_increasing([]).
check_increasing([_]).
@AhmadElsagheer
AhmadElsagheer / joruney.pl
Created December 30, 2017 02:14
Train Joruney (CLPFD)
:-use_module(library(clpfd)).
trains([[1,2,0,1], % from station, to station, departs at, arrives at
[1,4,0,5],
[2,3,4,5],
[2,3,0,1],
[3,4,5,6],
[3,4,2,3],
[3,4,8,9]]).
@AhmadElsagheer
AhmadElsagheer / promote1.pl
Created December 30, 2017 02:28
Employees Promotoion
:-use_module(library(clpfd)).
employees([
[1, 75, 0, 30, 25],
[2, 83, 0, 45, 25],
[3, 90, 1, 45, 50],
[4, 45, 3, 75, 25],
[5, 89, 0, 52, 50]
]).
@AhmadElsagheer
AhmadElsagheer / children.pl
Created December 30, 2017 07:34
Quarreling Children
:-use_module(library(clpfd)).
enemy_pairs([[1,2], [4,6], [4,7], [4, 9],[9,11], [12, 14], [14,16]]).
length_(Len, List):- length(List, Len).
children(Grid):-
length(Grid, 4),
maplist(length_(4), Grid),
maplist(domain, Grid),
@AhmadElsagheer
AhmadElsagheer / Board1.java
Created December 30, 2017 12:00
Sawmill Board Production (Final 2016 - Ex1)
// Backtracking solution + tracing using helper global variables
import java.io.IOException;
import java.util.Scanner;
public class Board1 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int A = sc.nextInt(), B = sc.nextInt(), C = sc.nextInt();
@AhmadElsagheer
AhmadElsagheer / hospital.pl
Created December 31, 2017 00:32
Hospital Patients (CLPFD)
:-use_module(library(clpfd)).
% patient(ID, Name, YearAdmitted, MonthAdmitted,
% DayAdmitted, HourAdmitted, MinuteAdmitted, Status, Payment)
patient(1, 'Bob Jones', 2014, 10, 1, 4, 55, 0, 2).
patient(2, 'Sally Smith', 2014, 9, 29, 5, 15, 1, 0).
patient(3, 'Ted Overton', 2014, 9, 30, 14, 15, 0, 0).
patient(4, 'Arnold Abouja', 2014, 10, 1, 5, 0, 0, 0).
patient(5, 'Seth Humbolt', 2014, 10, 1, 5, 10, 0, 0).
@AhmadElsagheer
AhmadElsagheer / board.pl
Created December 31, 2017 02:54
Sawmill Board Production
:-use_module(library(clpfd)).
patterns([
pattern(2, 0, 1, 0),
pattern(1, 2, 0, 0),
pattern(1, 1, 1, 1),
pattern(1, 0, 3, 0),
pattern(0, 3, 0, 1),
pattern(0, 2, 2, 0),
pattern(0, 1, 3, 1),
@AhmadElsagheer
AhmadElsagheer / seesaw.pl
Created December 31, 2017 03:23
See-Saw Balance
:-use_module(library(clpfd)).
% valid positions are in [-Saw_L, Saw_L].
% works for many variable weights and many variable positions.
seesaw_balance(Saw_L, Weights, Positions):-
length(Weights, N), length(Positions, N), NSaw_L is -Saw_L,
Weights ins 0..500, Positions ins NSaw_L..Saw_L,
angular_moment(Weights, Positions, 0),
apart_positions(Positions),
label(Positions), label(Weights).