Skip to content

Instantly share code, notes, and snippets.

@xypaul
Created May 27, 2015 09:10
Show Gist options
  • Save xypaul/0da0af629451bcc2125e to your computer and use it in GitHub Desktop.
Save xypaul/0da0af629451bcc2125e to your computer and use it in GitHub Desktop.
101 - The Blocks Problem
import java.io.*;
import java.util.*;
public class Main {
private static Stack[] Blocks;
private static PrintWriter out;
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
out = new PrintWriter(System.out, true);
// read first line
int items = Integer.valueOf(in.nextLine());
Blocks = new Stack[items];
for (int i = 0; i < items; i++) {
Blocks[i] = new Stack<Integer>();
Blocks[i].push(i);
}
String Line = "";
// Do a loop thing
while(!(Line = in.nextLine()).equals("quit")) {
String[]Splited = Line.split("\\s+");
Integer a = Integer.valueOf(Splited[1]);
Integer b = Integer.valueOf(Splited[3]);
if (findPosition(a) != findPosition(b)){
if (Splited[0].trim().equals("move")) {
if (Splited[2].trim().equals("onto"))
moveOnto(a,b);
if (Splited[2].trim().equals("over"))
moveOver(a,b);
}
if (Splited[0].trim().equals("pile")) {
if (Splited[2].trim().equals("onto"))
pileOnto(a,b);
if (Splited[2].trim().equals("over"))
pileOver(a,b);
}
}
}
for (int i = 0; i < items; i++) {
String item = "";
Stack<Integer> Tom = Blocks[i];
for (Integer el : Tom) {
item = item + " " + el;
}
out.printf(i + " :" + item + "\n");
}
}
public static void moveOnto(Integer a, Integer b) {
// clear any block on top of a back to their original position
clearAbove(b);
// same as moveOver
moveOver(a, b);
}
public static void moveOver(Integer a, Integer b) {
// put all blocks above b back into their original position
clearAbove(a);
// move block a onto top of stack containing b
Blocks[findPosition(b)].push(Blocks[findPosition(a)].pop());
}
public static void pileOnto(Integer a, Integer b) {
// move blocks on top of b to their original position
clearAbove(b);
// same as pileOver
pileOver(a, b);
}
public static void pileOver(Integer a, Integer b) {
// move all blocks above a and including a onto the top of stack containing b
Stack<Integer> currentBlock = Blocks[findPosition(a)];
Stack<Integer> moveToBlock = Blocks[findPosition(b)];
for (Integer i = findIndex(a); i < currentBlock.size(); i++) {
Integer item = currentBlock.get(i);
moveToBlock.push(item);
}
}
public static void clearAbove(Integer a) {
Stack<Integer> currentBlock = Blocks[findPosition(a)];
for (Integer i = findIndex(a) + 1; i < currentBlock.size(); i++) {
// Get item and add it to original block position
Integer item = currentBlock.get(i);
Blocks[item].push(item);
currentBlock.remove(i); // Remove item
}
}
public static Integer findPosition(Integer a) {
for (Integer i = 0; i < Blocks.length; i++) {
if (Blocks[i].contains(a)){
return i;
}
}
return -1;
}
public static Integer findIndex(Integer a) {
return Blocks[findPosition(a)].indexOf(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment