day07
earnest ma 2021-12-05 14:19:39 -05:00
parent 2b04ab0cbe
commit 8a1ce1e0f5
Signed by: earnest ma
GPG Key ID: A343F43342EB6E2A
2 changed files with 1078 additions and 0 deletions

1000
day03-input.txt Normal file

File diff suppressed because it is too large Load Diff

78
day03.java Normal file
View File

@ -0,0 +1,78 @@
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);
}
}