Programming Geek
Rated 4.1/5 based on 446 reviews

### Pebble Merchant : Problem #7 ( Aspirations 2020)

The problem is simple, It just needs a simple mathematics of calculating the area given the
coordinates of several vertices of a polygon:

The input file as given in the contest was:

```package test.pebblemerchant;

public class PebbleMerchant {

public int[ ] findCost(char[ ] direction,int[ ] length)
{
//write code here
int a[] = {0,0};
return a;
}

public static void main(String args[])
{
PebbleMerchant pm = new PebbleMerchant();

// example1
{
char[] direction = {'L','R','R','L','L','R','R','R','R','L','Z'};
int[] length = {2,2,1,1,1,2,5,2,2,3,1};

int res[] = pm.findCost(direction, length);

for (int i = 0; i < res.length; i++)
{
System.out.println(res[i]);
}
}

// example3
{
char[] direction = {'L','R','L','R','R','L','R','R','L','R','A','L','Z'};
int[] length = {1,2,2,2,2,2,5,2,1,2,3,2,2};

int res[] = pm.findCost(direction, length);

for (int i = 0; i < res.length; i++)
{
System.out.println(res[i]);
}
}
}
}

```
The solution is:
```package test.pebblemerchant;

import java.awt.Point;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

public class PebbleMerchant {

public int[] findCost(char[] direction, int[] length) {
int a[] = {0, 0};
boolean flag = false;
flag = isInvalid(direction, length);
if (flag == false) {
return a;
} else {
int area = getResult(direction, length);
int cost = area * 15;
a[0] = area;
a[1] = cost;
return a;
}
}

public boolean isInvalid(char[] dir, int[] len) {
boolean flag = false;

if (dir == null || len == null) {

return flag;
} else if (dir.length != len.length) {

return flag;
} else if (!(dir[dir.length - 1] == 'Z')) {

return flag;
} else {

for (int i = 0; i < len.length; i++) {
if (len[i] <= 0) {
return flag;
}

}

for (int i = 0; i < len.length - 1; i++) {
if (dir[i] == 'L' || dir[i] == 'R') {
continue;
} else {
return flag;
}

}
}

return true;

}

public int getResult(char[] direction, int[] length) {

int x = 0;
int y = 0;
Vik vik = new Vik();
ArrayList pArrayList = new ArrayList();
for (int i = 0; i < direction.length; i++) {
if (direction[i] == 'Z') {
Point p = new Point(0, 0);
break;
}
String next = vik.getNext(direction[i] + "");

if (next.equals("X")) {
x -= length[i];
Point p = new Point(x, y);
vik.curr = "W";
}
if (next.equals("XX")) {
x += length[i];
Point p = new Point(x, y);
vik.curr = "E";
}
if (next.equals("Y")) {
y += length[i];

Point p = new Point(x, y);
vik.curr = "N";
}
if (next.equals("YY")) {
y -= length[i];
Point p = new Point(x, y);
vik.curr = "S";
}

}

int area = 0;
for (int i = 0; i < pArrayList.size(); i++) {
if (i == pArrayList.size() - 1) {
Point p = pArrayList.get(i);
Point p1 = pArrayList.get(0);

area += p.getX() * p1.getY() - p.getY() * p1.getX();

} else {
Point p = pArrayList.get(i);
Point p1 = pArrayList.get(i + 1);
area += p.getX() * p1.getY() - p.getY() * p1.getX();
}

}
if (area < 0) {
area *= -1;
}
//  System.out.println("area=" + area / 2);
return area / 2;

}

private double area(Rectangle2D r) {
return r.getWidth() * r.getHeight();
}

class Vik {

String curr = "N";
String next;

String getNext(String str) {
str.trim();
if (curr.equals("N")) {
if (str.equals("L")) {
next = "X";
} else {
next = "XX";
}

}
if (curr.equals("W")) {
if (str.equals("L")) {
next = "YY";
} else {
next = "Y";
}
}
if (curr.equals("E")) {
if (str.equals("L")) {
next = "Y";
} else {
next = "YY";
}

}
if (curr.equals("S")) {
if (str.equals("L")) {
next = "XX";
} else {
next = "X";
}

}
return next;
}
}

public static void main(String args[]) {
PebbleMerchant pm = new PebbleMerchant();

// example1
{
char[] direction = {'L', 'R', 'R', 'L', 'L', 'R', 'R', 'R', 'R', 'L', 'Z'};
int[] length = {2, 2, 1, 1, 1, 2, 5, 2, 2, 3, 1};

int res[] = pm.findCost(direction, length);

for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}

}

{
char[] direction = {'L', 'L', 'R', 'R', 'L', 'R', 'R', 'L', 'R', 'L', 'R', 'R', 'L', 'R', 'L', 'R', 'R', 'L', 'Z'};
int[] length = {1, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 2, 2, 1, 1, 1, 1, 2, 2};
// char[] d={'L','R','R','R','Z'};
// int[] l={1,1,2,1,1};
//  pm.findCost(d, l);
int res[] = pm.findCost(direction, length);

for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}

}

{
char[] direction = {'L', 'R', 'L', 'R', 'R', 'L', 'R', 'R', 'L', 'R', 'A', 'L', 'Z'};
int[] length = {1, 2, 2, 2, 2, 2, 5, 2, 1, 2, 3, 2, 2};

int res[] = pm.findCost(direction, length);

for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}

}
}

```