Created
December 7, 2020 00:52
-
-
Save My2ndAngelic/70566ca50f4b547060a4a0ac79855a0f to your computer and use it in GitHub Desktop.
AoC 2020 day 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AoC2020; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.ArrayList; | |
import java.util.Locale; | |
import java.util.Scanner; | |
public class AoC4 { | |
public static void main(String[] args) { | |
ArrayList<String> al = new ArrayList<>(); | |
try { | |
al = passportImport(new File(System.getProperty("user.dir") + "/src/AoC2020/input/input4.txt")); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
int validpass1 = 0; | |
int validpass2 = 0; | |
for (String s : al) { | |
if (isValidPassportPart1(s)) { | |
validpass1++; | |
} | |
if (isValidPassport2(s)) { | |
validpass2++; | |
} | |
} | |
System.out.println("Part 1"); | |
System.out.println("Total passport: " + al.size()); | |
System.out.println("Valid passport: " + validpass1); | |
System.out.println("Part 2"); | |
System.out.println("Total passport: " + al.size()); | |
System.out.println("Valid passport: " + validpass2); | |
} | |
public static ArrayList<String> passportImport(File f) throws FileNotFoundException { | |
ArrayList<String> al = new ArrayList<>(); | |
al.add(""); | |
StringBuilder sb = new StringBuilder(); | |
Scanner s = new Scanner(f); | |
int i = 0; | |
while (s.hasNextLine()) { | |
String st = s.nextLine(); | |
if (!st.isEmpty()) { | |
sb.append(st).append(" "); | |
al.set(i, sb.toString()); | |
} else { | |
al.add(""); | |
sb = new StringBuilder(); | |
i++; | |
} | |
} | |
return al; | |
} | |
public static boolean isFieldExist(String s, String fieldname) { | |
return s.contains(fieldname); | |
} | |
public static boolean isValidPassportPart1(String s) { | |
return isFieldExist(s, "byr") && | |
isFieldExist(s, "iyr") && | |
isFieldExist(s, "eyr") && | |
isFieldExist(s, "hgt") && | |
isFieldExist(s, "hcl") && | |
isFieldExist(s, "ecl") && | |
isFieldExist(s, "pid"); | |
} | |
public static String getField(String s, String fieldname) { | |
String[] sa = s.split(" "); | |
if (isFieldExist(s, fieldname)) { | |
for (String value : sa) { | |
if (value.contains(fieldname)) { | |
return value.split(":")[1]; | |
} | |
} | |
} | |
return "-1"; | |
} | |
public static boolean isvalidBirthyear(String s) { | |
try { | |
int a = Integer.parseInt(getField(s, "byr")); | |
return 1920 <= a && a <= 2002; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
public static boolean isvalidIssueyear(String s) { | |
try { | |
int a = Integer.parseInt(getField(s, "iyr")); | |
return a >= 2010 && a <= 2020; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
public static boolean isvalidExpirationyear(String s) { | |
try { | |
int a = Integer.parseInt(getField(s, "eyr")); | |
return a >= 2020 && a <= 2030; | |
} catch (Exception e) { | |
return false; | |
} | |
} | |
public static boolean isvalidHeight(String s) { | |
String st = getField(s, "hgt"); | |
st = st.toLowerCase(); | |
if (st.length() == 4) { | |
if (st.charAt(2) == 'i' && st.charAt(3) == 'n') { | |
if (Character.isDigit(st.charAt(0)) && Character.isDigit(st.charAt(1))) { | |
int a = 10*Integer.parseInt(String.valueOf(st.charAt(0))) + Integer.parseInt(String.valueOf(st.charAt(1))); | |
return 59 <= a && a <= 76; | |
} | |
} | |
} else if (st.length() == 5) { | |
if (st.charAt(3) == 'c' && st.charAt(4) == 'm') { | |
if (Character.isDigit(st.charAt(0)) && Character.isDigit(st.charAt(1)) && Character.isDigit(st.charAt(2))) { | |
int a = 100*Integer.parseInt(String.valueOf(st.charAt(0))) + 10*Integer.parseInt(String.valueOf(st.charAt(1))) + Integer.parseInt(String.valueOf(st.charAt(2))); | |
return 150 <= a && a <= 193; | |
} | |
} | |
} | |
return false; | |
} | |
public static boolean isValidHaircolor(String s) { | |
String st = getField(s, "hcl"); | |
int counter = 6; | |
if (st.length() == 7) { | |
if (st.charAt(0) == '#') { | |
for (int i = 1; i < st.length(); i++) { | |
int a = st.charAt(i); | |
if ((48 <= a && a <= 57) || (65 <= a && a <= 70) || (97 <= a && a <= 102)) { | |
counter--; | |
} | |
} | |
} | |
} return counter==0; | |
} | |
public static boolean isValidEyecolor(String s) { | |
String st = getField(s, "ecl"); | |
return st.equals("amb") || | |
st.equals("blu") || | |
st.equals("brn") || | |
st.equals("gry") || | |
st.equals("grn") || | |
st.equals("hzl") || | |
st.equals("oth"); | |
} | |
public static boolean isValidPassportnumber(String s) { | |
String st = getField(s, "pid"); | |
if (st.length() == 9) { | |
for (char c : st.toCharArray()) { | |
if (Character.isDigit(c)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public static boolean isValidPassport2(String s) { | |
return isvalidBirthyear(s) && | |
isvalidIssueyear(s) && | |
isvalidExpirationyear(s) && | |
isvalidHeight(s) && | |
isValidHaircolor(s) && | |
isValidEyecolor(s) && | |
isValidPassportnumber(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment