Skip to content

Instantly share code, notes, and snippets.

@az3
Last active August 29, 2015 14:00
Reddit Challenge 2014 04 16 The ASCII Architect
import java.util.ArrayList;
import java.util.List;
public class RedditChallenge2 {
public static void main(String[] args) {
challengeWednesday();
}
private static void challengeWednesday() {
try {
// Algorithm; first write as normal line, then rotate it.
// Original (string "1abc");
// +
// ++
// ++-
// Rotated (90 degrees counter clockwise);
// -
// +++
// ++
// Normal line can be max 19 chars (9 for spaces, 10 for char j).
// Max length of a line is 240 chars (given in the puzzle).
// So a 2d char array of [20][240] can handle our inputs.
// Then we will rotate this matrix -> [240][20] as max size.
String inputLine = "j3f3e3e3d3d3c3cee3c3c3d3d3e3e3f3fjij3f3f3e3e3d3d3c3cee3c3c3d3d3e3e3fj";
//inputLine="4d3f3g2i2i2i3g3f4d"; //easter egg
//inputLine="4a3a3a2a2a5b1a1a1a1a5b2a2a3a3a4a"; //creepy smiley
//inputLine="abcdefghijihgfedcba9a9a9a9a9a9a9aabde3e4j4j3eedba"; // great pyramide and eiffel tower
char[][] inputMatrix2 = convertToMatrix(inputLine);
//parseMatrix(inputMatrix2);
char[][] result = rotate90left(inputMatrix2);
parseMatrix(result);
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
} catch (Exception e) {
e.printStackTrace();
}
}
private static char[][] convertToMatrix(String inputLine) {
try {
String charA = "+";
String charB = "++";
String charC = "++-";
String charD = "++--";
String charE = "++--*";
String charF = "++--**";
String charG = "++--***";
String charH = "++--***.";
String charI = "++--***..";
String charJ = "++--***...";
char[] inputChars = inputLine.toCharArray();
List<Character> numbers = new ArrayList<>();
numbers.add('0');
numbers.add('1');
numbers.add('2');
numbers.add('3');
numbers.add('4');
numbers.add('5');
numbers.add('6');
numbers.add('7');
numbers.add('8');
numbers.add('9');
int lettersOnly = inputLine.replaceAll("[^a-j]", "").length();
// For ease, instead of 240, we will use number of chars as max width.
char[][] inputMatrix = new char[lettersOnly][20];
int leadingSpaceCount = -1;
int rowNumber = 0;
for (char c : inputChars) {
String tmpLine = "";
if (numbers.contains(c)) {
leadingSpaceCount = Integer.parseInt("" + c);
} else {
switch (c) {
case 'a':
tmpLine = charA;
break;
case 'b':
tmpLine = charB;
break;
case 'c':
tmpLine = charC;
break;
case 'd':
tmpLine = charD;
break;
case 'e':
tmpLine = charE;
break;
case 'f':
tmpLine = charF;
break;
case 'g':
tmpLine = charG;
break;
case 'h':
tmpLine = charH;
break;
case 'i':
tmpLine = charI;
break;
case 'j':
tmpLine = charJ;
break;
default:
System.out.println("Error! Check chararacter: " + c);
}
String leadingSpaceString = "";
if (leadingSpaceCount > 0) {
for (int i = 0; i < leadingSpaceCount; i++) {
leadingSpaceString = leadingSpaceString + " ";
}
leadingSpaceCount = -1;
}
tmpLine = leadingSpaceString + tmpLine;
if (tmpLine.length() < 20) {
int trailingSpaceCount = 20 - tmpLine.length();
String trailingSpaceString = "";
if (trailingSpaceCount > 0) {
for (int i = 0; i < trailingSpaceCount; i++) {
trailingSpaceString = trailingSpaceString + " ";
}
}
tmpLine = tmpLine + trailingSpaceString;
}
char[] tmpLineChar = tmpLine.toCharArray();
inputMatrix[rowNumber++] = tmpLineChar;
}
}
return inputMatrix;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static void parseMatrix(char[][] matrix) {
for (char[] cs : matrix) {
for (char c : cs) {
System.out.print(c);
}
System.out.println("");
}
}
private static char[][] rotate90left(char[][] matrix) {
try {
// http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
int width = matrix.length;
int height = matrix[0].length;
char[][] ret = new char[height][width];
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
ret[i][j] = matrix[j][height - i - 1];
}
}
return ret;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment