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.
283 lines
12 KiB
Java
283 lines
12 KiB
Java
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.util.Arrays;
|
|
import java.util.Scanner;
|
|
|
|
class PassPortInfo implements Comparable<PassPortInfo> {
|
|
String info = "";
|
|
int index = 0;
|
|
String actualInfo = "";
|
|
|
|
public PassPortInfo() {
|
|
info = "";
|
|
index = 0;
|
|
actualInfo = "";
|
|
}
|
|
|
|
public PassPortInfo(String inputInfo, int inputIndex) {
|
|
info = inputInfo;
|
|
index = inputIndex;
|
|
actualInfo = "";
|
|
}
|
|
|
|
public void set(String inputInfo, int inputIndex) {
|
|
info = inputInfo;
|
|
index = inputIndex;
|
|
}
|
|
|
|
public void setActualInfo(String input) {
|
|
actualInfo = input;
|
|
}
|
|
|
|
public void tostring() {
|
|
System.out.print(info);
|
|
System.out.print(" ");
|
|
System.out.print(index);
|
|
System.out.print(" ");
|
|
System.out.print(actualInfo);
|
|
}
|
|
|
|
public int compareTo(PassPortInfo compareInfo) {
|
|
int compareQuantity = ((PassPortInfo) compareInfo).index;
|
|
return this.index - compareQuantity;
|
|
}
|
|
}
|
|
|
|
class DayFour {
|
|
|
|
interface ArrayFunctions {
|
|
public int arrayFunction(String inputString);
|
|
}
|
|
|
|
ArrayFunctions[] mrArrayFunction = new ArrayFunctions[] { new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("byr");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("iyr");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("eyr");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("hgt");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("hcl");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("ecl");
|
|
}
|
|
}, new ArrayFunctions() {
|
|
public int arrayFunction(String inputString) {
|
|
return inputString.indexOf("pid");
|
|
}
|
|
} };
|
|
|
|
public static void main(String[] args) {
|
|
int repTimes = 0;
|
|
String mrTesterString = "";
|
|
int validCount = 0;
|
|
PassPortInfo[] infos;
|
|
infos = new PassPortInfo[8];
|
|
for (int i = 0; i < 8; i++) {
|
|
infos[i] = new PassPortInfo();
|
|
}
|
|
String[] infoNames = new String[] { "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid" };
|
|
try {
|
|
File inputFile = new File("input.txt");
|
|
Scanner readFile = new Scanner(inputFile);
|
|
while (readFile.hasNextLine()) {
|
|
String inputLine = readFile.nextLine();
|
|
if (containsSemicolon(inputLine)) {
|
|
// System.out.println(inputLine);
|
|
mrTesterString = mrTesterString + inputLine;
|
|
// System.out.println(mrTesterString);
|
|
} else {
|
|
// System.out.println(mrTesterString);
|
|
if ((countSemicolon(mrTesterString) == 8)
|
|
|| ((countSemicolon(mrTesterString) == 7) && (!(containsCid(mrTesterString))))) {
|
|
// System.out.println(removeSpaces(mrTesterString));
|
|
if (containsCid(mrTesterString)) {
|
|
repTimes = 8;
|
|
} else {
|
|
repTimes = 7;
|
|
infos[7].index = 400;
|
|
}
|
|
// System.out.print(repTimes);
|
|
// System.out.print(" ");
|
|
for (int i = 0; i < repTimes; i++) {
|
|
infos[i].set(infoNames[i], mrTesterString.indexOf(infoNames[i]));
|
|
// infos[i].tostring();
|
|
// System.out.println();
|
|
}
|
|
Arrays.sort(infos);
|
|
for (int i = 0; i < repTimes - 1; i++) {
|
|
infos[i].actualInfo = removeSpaces(
|
|
(mrTesterString.substring(infos[i].index + 4, infos[i + 1].index)));
|
|
// infos[i].tostring();
|
|
// System.out.println();
|
|
}
|
|
infos[repTimes - 1].actualInfo = removeSpaces(
|
|
(mrTesterString.substring(infos[repTimes - 1].index + 4)));
|
|
// System.out.println(mrTesterString);
|
|
// validCount++;
|
|
for (int i = 0; i < infos.length; i++) {
|
|
// infos[i].tostring();
|
|
// System.out.print(" ");
|
|
if (i % 8 == 0) {
|
|
// System.out.println();
|
|
}
|
|
}
|
|
int fCount = 0;
|
|
for (int i = 0; i < repTimes; i++) {
|
|
// System.out.print(infos[i].actualInfo);
|
|
// System.out.print(" ");
|
|
if ((infos[i].info == "byr") && (Integer.parseInt(infos[i].actualInfo) >= 1920
|
|
&& Integer.parseInt(infos[i].actualInfo) <= 2002)) {
|
|
fCount++;
|
|
// System.out.print("byr triggered ");
|
|
}
|
|
if ((infos[i].info == "iyr") && (Integer.parseInt(infos[i].actualInfo) >= 2010
|
|
&& Integer.parseInt(infos[i].actualInfo) <= 2020)) {
|
|
fCount++;
|
|
// System.out.print("iyr triggered ");
|
|
}
|
|
if ((infos[i].info == "eyr") && (Integer.parseInt(infos[i].actualInfo) >= 2020
|
|
&& Integer.parseInt(infos[i].actualInfo) <= 2030)) {
|
|
fCount++;
|
|
// System.out.print("eyr triggered ");
|
|
}
|
|
if (infos[i].info == "hgt") {
|
|
CharSequence metric = "cm";
|
|
CharSequence american = "in";
|
|
String height = infos[i].actualInfo.substring(0, infos[i].actualInfo.length() - 2);
|
|
if (infos[i].actualInfo.contains(metric)
|
|
&& (150 <= Integer.parseInt(height) && Integer.parseInt(height) <= 193)) {
|
|
fCount++;
|
|
// System.out.print("hgt triggered ");
|
|
}
|
|
if (infos[i].actualInfo.contains(american)
|
|
&& (59 <= Integer.parseInt(height) && Integer.parseInt(height) <= 76)) {
|
|
fCount++;
|
|
// System.out.print("hgt triggered ");
|
|
}
|
|
}
|
|
if (infos[i].info == "hcl" && infos[i].actualInfo.indexOf('#') == 0) {
|
|
String mrChecker = infos[i].actualInfo.substring(1);
|
|
char[] mrCharArray;
|
|
int checker = 0;
|
|
int mCount = 0;
|
|
mrCharArray = mrChecker.toCharArray();
|
|
for (int j = 0; j < mrCharArray.length; j++) {
|
|
checker = mrCharArray[j];
|
|
// System.out.print(checker);
|
|
// System.out.print(" ");
|
|
// System.out.print(mrCharArray[j]);
|
|
// System.out.println();
|
|
if ((checker >= 97 && checker <= 102) || (checker >= 48 && checker <= 57)) {
|
|
// System.out.print(checker);
|
|
// System.out.print(" ");
|
|
// System.out.print(mrCharArray[j]);
|
|
// System.out.println();
|
|
mCount++;
|
|
}
|
|
|
|
}
|
|
if (mCount == 6) {
|
|
fCount++;
|
|
System.out.print("hcl triggered ");
|
|
}
|
|
}
|
|
if (infos[i].info == "ecl") {
|
|
CharSequence color1 = "amb";
|
|
CharSequence color2 = "blu";
|
|
CharSequence color3 = "brn";
|
|
CharSequence color4 = "gry";
|
|
CharSequence color5 = "grn";
|
|
CharSequence color6 = "hzl";
|
|
CharSequence color7 = "oth";
|
|
String mrChecker = infos[i].actualInfo;
|
|
if (mrChecker.contains(color1) || mrChecker.contains(color2)
|
|
|| mrChecker.contains(color3) || mrChecker.contains(color4)
|
|
|| mrChecker.contains(color5) || mrChecker.contains(color6)
|
|
|| mrChecker.contains(color7)) {
|
|
fCount++;
|
|
// System.out.print("ecl triggered ");
|
|
}
|
|
}
|
|
if (infos[i].info == "pid" && infos[i].actualInfo.length() == 9) {
|
|
fCount++;
|
|
// System.out.print("pid triggered ");
|
|
}
|
|
// System.out.println();
|
|
}
|
|
System.out.println();
|
|
if (fCount == 7) {
|
|
validCount++;
|
|
}
|
|
}
|
|
mrTesterString = "";
|
|
}
|
|
}
|
|
readFile.close();
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println("crapeth");
|
|
}
|
|
System.out.println(validCount);
|
|
|
|
}
|
|
|
|
public static boolean containsSemicolon(String inputString) {
|
|
char[] mrCharArray;
|
|
mrCharArray = inputString.toCharArray();
|
|
boolean mrBool = false;
|
|
for (int i = 0; i < mrCharArray.length; i++) {
|
|
if (mrCharArray[i] == ':') {
|
|
return true;
|
|
}
|
|
}
|
|
// System.out.print("Contains");
|
|
return mrBool;
|
|
}
|
|
|
|
public static int countSemicolon(String inputString) {
|
|
// System.out.println(inputString);
|
|
char[] mrCharArray;
|
|
mrCharArray = inputString.toCharArray();
|
|
int colonCount = 0;
|
|
for (int i = 0; i < mrCharArray.length; i++) {
|
|
if (mrCharArray[i] == ':') {
|
|
colonCount++;
|
|
}
|
|
}
|
|
// System.out.println(colonCount);
|
|
return colonCount;
|
|
|
|
}
|
|
|
|
public static boolean containsCid(String inputString) {
|
|
CharSequence whale = "cid";
|
|
// System.out.print("Contains");
|
|
return inputString.contains(whale);
|
|
}
|
|
|
|
public static String removeSpaces(String inputString) {
|
|
String outputString = "";
|
|
char[] mrCharArray;
|
|
mrCharArray = inputString.toCharArray();
|
|
for (int i = 0; i < mrCharArray.length; i++) {
|
|
if (mrCharArray[i] != ' ') {
|
|
outputString = outputString + mrCharArray[i];
|
|
}
|
|
}
|
|
return outputString;
|
|
}
|
|
|
|
}
|