Skip to content

Instantly share code, notes, and snippets.

@arestifo
Created May 29, 2015 16:37
Show Gist options
  • Select an option

  • Save arestifo/fe94fa62f265edb0f34b to your computer and use it in GitHub Desktop.

Select an option

Save arestifo/fe94fa62f265edb0f34b to your computer and use it in GitHub Desktop.
Obfuscated matrix multiplier
package com.bcchack.ap;
import static java.lang.System.out;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class TesterMatrices {
//static int[][] m1 = {{1, 2, -2, 0}, {-3, 4, 7, 2}, {6, 0, 3, 1}};
//static int[][] m2 = {{-1, 3}, {0, 9}, {1, -11}, {4, -5}};
public static void main(String[] args) throws FileNotFoundException
{
Scanner scan = new Scanner(new File("H:\\java_testing\\MatrixData.txt"));
ArrayList<ArrayList<ArrayList<Integer>>> matrices = new ArrayList<ArrayList<ArrayList<Integer>>>();
int mCount = -1, rCount = -1;
while (scan.hasNextLine())
{
String line = scan.nextLine();
if (line.equals("matrix"))
{
matrices.add(new ArrayList<ArrayList<Integer>>());
mCount++;
rCount = -1;
}
else if (line.equals("row"))
{
matrices.get(mCount).add(new ArrayList<Integer>());
rCount++;
}
else
{
matrices.get(mCount).get(rCount).add(Integer.parseInt(line));
}
}
int[][] a = new int[matrices.get(0).size()][matrices.get(0).get(0).size()];
int[][] b = new int[matrices.get(1).size()][matrices.get(1).get(0).size()];
for (int m1 = 0; m1 < matrices.size(); m1++)
{
int[][] temp = m1 == 0 ? a : b;
for (int m2 = 0; m2 < matrices.get(m1).size(); m2++)
{
for (int m3 = 0; m3 < matrices.get(m1).get(m2).size(); m3++)
{
temp[m2][m3] = matrices.get(m1).get(m2).get(m3);
}
}
if (m1 == 0)
a = temp;
else if (m1 == 1)
b = temp;
}
int[][] product = MatrixMultiply.multiplyMatrices(a, b);
printMatrix(product);
}
private static void printMatrix(int[][] matrix)
{
for (int[] dd : matrix)
{
for (int d : dd)
{
out.printf("%d%s", d, "\t");
}
out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment