You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6 KiB
Java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class DayThree {
public static void main(String[] args) {
int treeCount = 0;
int xLoc = 0;
int yLoc = 0;
int maxRows = 1;
int xStep = 1;
int yStep = 2;
int[] rowTreeIndex;
int[][] trees;
int indexCounter = 0;
trees = new int[400][31];
try {
File inputFile = new File("input.txt");
Scanner readFile = new Scanner(inputFile);
while (readFile.hasNextLine()) {
String inputLine = readFile.nextLine();
rowTreeIndex = findTrees(inputLine);
for (int i = 0; i < rowTreeIndex.length; i++) {
trees[indexCounter][i] = rowTreeIndex[i];
// System.out.println(trees[indexCounter][i]);
}
indexCounter++;
maxRows++;
}
// System.out.println(maxRows);
readFile.close();
} catch (FileNotFoundException e) {
System.out.println("crapeth");
}
while (yLoc != maxRows) {
// System.out.println(xLoc % 31);
if (hitTree(trees[yLoc], xLoc % 31, yLoc)) {
treeCount++;
}
xLoc = xStep + xLoc;
yLoc = yStep + yLoc;
}
System.out.println(treeCount);
}
public static int[] findTrees(String inputString) {
int indexCount = 0;
int[] treeIndex;
char[] stringToChar;
treeIndex = fill();
stringToChar = inputString.toCharArray();
for (int i = 0; i < inputString.length(); i++) {
// System.out.println(stringToChar[i]);
if (stringToChar[i] == '#') {
treeIndex[indexCount] = i;
indexCount++;
}
}
return treeIndex;
}
public static boolean hitTree(int[] inputArray, int inputX, int inputY) {
boolean treeHit = false;
// System.out.println(inputY);
// System.out.print(" ");
for (int i = 0; i < inputArray.length; i++) {
// System.out.print(inputArray[i]);
// System.out.print(" ");
}
System.out.println();
for (int i = 0; i < inputArray.length; i++) {
if (inputX == inputArray[i]) {
treeHit = true;
}
}
return treeHit;
}
public static int[] fill() {
int[] whale;
whale = new int[31];
for (int i = 0; i < 31; i++) {
whale[i] = 40;
}
return whale;
}
}