epicfail
BAN USERimport java.util.LinkedList;
import java.util.List;
public class NByNGridWord {
public static void main(String[] args){
char [][]grid =
{{'b','a','d'},
{'y','i','r'},
{'b','m','g'}};
LinkedList<Coordinate> crds = new LinkedList<Coordinate>();
System.out.println("there is a match: "+isThereAMatch(grid,"aim",crds));
for(Coordinate c: crds){
System.out.println("coordinate:("+c.i+","+c.j+")");
}
}
public static boolean isThereAMatch(char[][] grid, String word,LinkedList<Coordinate> crds){
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[i].length;j++){
if(grid[i][j]==word.charAt(0)){
if(matchesHorizantally(grid,word,i,j,crds)){
return true;
}
if(matchesVertically(grid,word,i,j,crds)){
return true;
}
if(matchesDiagonally(grid,word,i,j,crds)){
return true;
}
}
}
}
return false;
}
public static boolean matchesHorizantally(char[][] grid, String word, int i, int j,LinkedList<Coordinate> crds){
LinkedList<Coordinate> foundCrds = new LinkedList<Coordinate>();
for(int n=0;n<word.length()&&j+n<grid[i].length;n++){
if(grid[i][j+n]!=word.charAt(n)){
break;
}else{
foundCrds.add(new Coordinate(i,j+n));
}
}
//found the word or else check horizontal left direction
if(foundCrds.size()==word.length()){
crds.addAll(foundCrds);
return true;
}else{
foundCrds.clear();
for(int n=0;n<word.length()&&j-n>=0;n++){
if(grid[i][j-n]!=word.charAt(n)){
return false;
}else{
foundCrds.add(new Coordinate(i,j-n));
}
}
}
crds.addAll(foundCrds);
return true;
}
public static boolean matchesVertically(char[][] grid, String word,int i, int j,LinkedList<Coordinate> crds){
LinkedList<Coordinate> foundCrds = new LinkedList<Coordinate>();
for(int n=0;n<word.length()&&i+n<grid.length;n++){
if(grid[i+n][j]!=word.charAt(n)){
break;
}else{
foundCrds.add(new Coordinate(i+n,j));
}
}
//found the word or else check horizontal left direction
if(foundCrds.size()==word.length()){
crds.addAll(foundCrds);
return true;
}else{
foundCrds.clear();
for(int n=0;n<word.length()&&i-n>=0;n++){
if(grid[i-n][j]!=word.charAt(n)){
return false;
}else{
foundCrds.add(new Coordinate(i-n,j));
}
}
}
crds.addAll(foundCrds);
return true;
}
public static boolean matchesDiagonally(char[][] grid, String word,int i,int j,LinkedList<Coordinate> crds){
LinkedList<Coordinate> foundCrds = new LinkedList<Coordinate>();
for(int n=0;n<word.length()&&i+n<grid.length&&j+n<grid[i].length;n++){
if(grid[i+n][j+n]!=word.charAt(n)){
break;
}else{
foundCrds.add(new Coordinate(i+n,j+n));
}
}
//found the word or else check horizontal left direction
if(foundCrds.size()==word.length()){
crds.addAll(foundCrds);
return true;
}else{
foundCrds.clear();
}
for(int n=0;n<word.length()&&i-n>=0&&j+n<grid[i].length;n++){
if(grid[i-n][j+n]!=word.charAt(n)){
break;
}else{
foundCrds.add(new Coordinate(i-n,j+n));
}
}
//found the word or else check horizontal left direction
if(foundCrds.size()==word.length()){
crds.addAll(foundCrds);
return true;
}else{
foundCrds.clear();
}
for(int n=0;n<word.length()&&i+n<grid.length&&j-n>=0;n++){
if(grid[i+n][j-n]!=word.charAt(n)){
break;
}else{
foundCrds.add(new Coordinate(i+n,j-n));
}
}
//found the word or else check horizontal left direction
if(foundCrds.size()==word.length()){
crds.addAll(foundCrds);
return true;
}else{
foundCrds.clear();
}
for(int n=0;n<word.length()&&i-n>=0&&j-n>=0;n++){
if(grid[i-n][j-n]!=word.charAt(n)){
return false;
}else{
foundCrds.add(new Coordinate(i-n,j-n));
}
}
crds.addAll(foundCrds);
return true;
}
public static class Coordinate{
public int i;
public int j;
public Coordinate(int i, int j){
this.i = i;
this.j = j;
}
}
}
- epicfail November 08, 2015import java.util.HashSet;
public class Colorful {
public static void main(String[] args){
int number =236;
System.out.println(number+" is colorful: "+isColorful(number));
}
public static boolean isColorful(int number){
int product=1;
String numString = String.valueOf(number);
HashSet<Integer> products = new HashSet<Integer>();
int numOfProducts=0;
for(int i=0;i<numString.length()-1;i++){
products.add(Integer.parseInt(numString.substring(i,i+1)));
products.add(Integer.parseInt(numString.substring(i,i+1))*Integer.parseInt(numString.substring(i+1,i+2)));
product*=Integer.parseInt(numString.substring(i,i+1));
numOfProducts+=2;
}
//handle last number
product*=Integer.parseInt(numString.substring(numString.length()-1,numString.length()));
products.add(product);
products.add(Integer.parseInt(numString.substring(numString.length()-1,numString.length())));
numOfProducts+=2;
if(numOfProducts==products.size()){
return true;
}else{
return false;
}
}
}
- epicfail November 07, 2015import java.util.Scanner;
public class Glitch {
public static void main(String[] args){
//program specification asks thats you prompt for input
Scanner keyboard = new Scanner(System.in);
System.out.print("x = ");
int x = keyboard.nextInt();
System.out.print("y = ");
int y = keyboard.nextInt();
System.out.print("total steps = ");
int totalSteps = keyboard.nextInt();
//run the Glitch Robot
GlitchRobot glitch = new GlitchRobot(x,y,totalSteps);
glitch.run();
System.out.println(glitch);
}
private static class GlitchRobot{
int totalSteps;
int startLocation;
int curLocation;
int x;
int y;
int direction;//either 1 or -1
public GlitchRobot(int x, int y,int totalSteps){
this.totalSteps = totalSteps;
startLocation = 0;
curLocation = 0;
this.x = x;
this.y = y;
direction = 1;//starts in positive direction (out multiplier)
}
public void run(){
int stepCounter = 0;
int stepIterator = 0;
while(stepCounter<totalSteps){
curLocation += (x * (stepIterator+1)) * direction;
stepCounter += (x * (stepIterator+1));
if(stepCounter>=totalSteps){
//we could be past totals steps so break
break;
}
//if y divides stepCounter turn 180 degrees. Since there are two different types of steps we have to compute it twice
if( y % stepCounter == 0)
direction*=-1;
curLocation -= (x + (stepIterator+1)) * direction;
stepCounter += (x + (stepIterator+1));
if(y % stepCounter == 0)
direction*=-1;
stepIterator++;
}
}
@Override
public String toString(){
return "steps from starting point: "+Math.abs(curLocation-startLocation);
}
}
}
- epicfail November 08, 2015