Skip to content

Instantly share code, notes, and snippets.

@go-ive
Created September 17, 2013 13:39
Show Gist options
  • Save go-ive/6594415 to your computer and use it in GitHub Desktop.
Save go-ive/6594415 to your computer and use it in GitHub Desktop.
Reddit DailyProgrammer Challenge #133 - Chain Reaction
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class ChainReaction {
static int stepCount = 0;
enum Direction {
UP,
DOWN,
LEFT,
RIGHT
}
static class Element {
int posX;
int posY;
int radius;
char name;
boolean isActive;
Set<Direction> directions = new HashSet<Direction>();
Element[][] grid;
void propagate() {
if (!isActive) {
name = 'X';
drawGrid(grid.length, grid);
isActive = true;
for (Direction direction : directions) {
try {
int tempRadius = radius;
if (direction == Direction.UP) {
int tempPos = 1;
while (tempRadius-- > 0) {
if (grid[posX][posY - tempPos] != null) {
grid[posX][posY - tempPos].propagate();
}
tempPos++;
}
} else if (direction == Direction.DOWN) {
int tempPos = 1;
while (tempRadius-- > 0) {
if (grid[posX][posY + tempPos] != null) {
grid[posX][posY + tempPos].propagate();
}
tempPos++;
}
} else if (direction == Direction.LEFT) {
int tempPos = 1;
while (tempRadius-- > 0) {
if (grid[posX - tempPos][posY] != null) {
grid[posX - tempPos][posY].propagate();
}
tempPos++;
}
} else if (direction == Direction.RIGHT) {
int tempPos = 1;
while (tempRadius-- > 0) {
if (grid[posX + tempPos][posY] != null) {
grid[posX + tempPos][posY].propagate();
}
tempPos++;
}
}
} catch (IndexOutOfBoundsException e) {
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] numbers = sc.nextLine().split(" ");
int gridSize = Integer.parseInt(numbers[1]);
Element[][] grid = new Element[gridSize][gridSize];
List<Element> elements = new ArrayList<Element>();
char elementName = 'A';
for (int i = 0; i < Integer.parseInt(numbers[0]); i++) {
String[] elementLineItems = sc.nextLine().split(" ");
Element element = new Element();
element.grid = grid;
element.name = elementName++;
element.posX = Integer.parseInt(elementLineItems[0]);
element.posY = Integer.parseInt(elementLineItems[1]);
element.radius = Integer.parseInt(elementLineItems[2]);
if (elementLineItems[3].toUpperCase().contains("U"))
element.directions.add(Direction.UP);
if (elementLineItems[3].toUpperCase().contains("D"))
element.directions.add(Direction.DOWN);
if (elementLineItems[3].toUpperCase().contains("L"))
element.directions.add(Direction.LEFT);
if (elementLineItems[3].toUpperCase().contains("R"))
element.directions.add(Direction.RIGHT);
elements.add(element);
}
sc.close();
for (Element element : elements) {
grid[element.posX][element.posY] = element;
}
drawGrid(gridSize, grid);
elements.get(0).propagate();
}
private static void drawGrid(int gridSize, Element[][] grid) {
System.out.println("Step " + stepCount++ + ":");
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
if (grid[j][i] != null) {
System.out.print(grid[j][i].name);
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment