aoc/day03.java

79 lines
1.6 KiB
Java
Raw Normal View History

2021-12-05 14:19:39 -05:00
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class day03 {
public static void main(String[] args) throws IOException {
String gammaCalc = "";
String epsilonCalc = "";
int gammaVal = 0;
int epsilonVal = 0;
int finalAns = 0;
String line;
BufferedReader in = new BufferedReader(new FileReader("day03-input.txt"));
line = in.readLine();
int numLengthHuman = line.length();
// [0] will not have anything i think
int[] zeroCounter = new int[numLengthHuman + 1];
int[] oneCounter = new int[numLengthHuman + 1];
// temporary var, concat into an int for gammaVal/ epsilonVal.
String[] tempC = new String[numLengthHuman + 1];
while (line != null) {
for (int i = 1; i <= line.length(); i++) {
int currentNum = Integer.valueOf(line.substring(i - 1, i));
if (currentNum == 0) {
zeroCounter[i]++;
}
if (currentNum == 1) {
oneCounter[i]++;
}
}
line = in.readLine();
}
in.close();
// calc gamma
for (int i = 1; i <= numLengthHuman; i++) {
if (zeroCounter[i] > oneCounter[i]) {
tempC[i] = "0";
} else {
tempC[i] = "1";
}
gammaCalc = gammaCalc + tempC[i];
}
gammaVal = Integer.parseInt(gammaCalc, 2); // some weird conversion thing?
// calc epsilon
for (int i = 1; i <= numLengthHuman; i++) {
if (zeroCounter[i] > oneCounter[i]) {
tempC[i] = "1"; // this time, one is the least
} else {
tempC[i] = "0";
}
epsilonCalc = epsilonCalc + tempC[i];
}
epsilonVal = Integer.parseInt(epsilonCalc, 2);
finalAns = gammaVal * epsilonVal;
System.out.println(finalAns);
}
}