r/adventofcode • u/daggerdragon • Dec 04 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 04 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- T-2 days until unlock!
- Full details and rules are in the Submissions Megathread
--- Day 04: Passport Processing ---
Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:12:55, megathread unlocked!
91
Upvotes
View all comments
2
u/canaguia Dec 04 '20
Java solution i did in Intellij. Definetally need help making some of my methods more efficient, pointers? (only part 2 cause i couldnt fit all of it)
public class passportChecker {public ArrayList<HashMap<String, String>> createPassportList(String filename) {BufferedReader inputStream = null;ArrayList<HashMap<String, String>> passportList = new ArrayList<>();try {inputStream = new BufferedReader(new FileReader(filename));String line;int passportNumber = 0;passportList.add(new HashMap<String, String>());while ((line = inputStream.readLine()) != null) {if (line.isEmpty()){passportNumber++;passportList.add(new HashMap<String, String>());continue;}String[] lineData = line.split(":|\\s");for (int s = 0; s < lineData.length; s += 2) {passportList.get(passportNumber).put(lineData[s],lineData[s + 1]);}}} catch (IOException ioe) {System.out.println(ioe.getMessage());}return passportList;}private int returnValid(ArrayList<HashMap<String, String>> passList){int numbValid = 0;for (HashMap<String, String> h : passList){if (testValidity(h)){numbValid++;}}return numbValid;}private boolean testValidity(HashMap<String, String> testPass){int validCheck = 0;if (testPass.containsKey("byr")){if ((Integer.parseInt(testPass.get("byr")) <= 2002) && (1920 <= Integer.parseInt(testPass.get("byr")))){validCheck++;}}if (testPass.containsKey("iyr")){if ((Integer.parseInt(testPass.get("iyr")) <= 2020) && (2010 <= Integer.parseInt(testPass.get("iyr")))){validCheck++;}}if (testPass.containsKey("eyr")){if ((Integer.parseInt(testPass.get("eyr")) <= 2030) && (2020 <= Integer.parseInt(testPass.get("eyr")))){validCheck++;}}if (testPass.containsKey("hgt")){if (testPass.get("hgt").length() >= 4) {if (testPass.get("hgt").indexOf("cm") == -1) {//inchesint suffixLocation = testPass.get("hgt").indexOf("in");int value = Integer.parseInt(testPass.get("hgt").substring(0, suffixLocation));if ((value <= 76) && (value >= 59)) {validCheck++;}} else {//centimeterint suffixLocation = testPass.get("hgt").indexOf("cm");int value = Integer.parseInt(testPass.get("hgt").substring(0, suffixLocation));if ((value <= 193) && (value >= 150)) {validCheck++;}}}}if (testPass.containsKey("hcl")){if (testPass.get("hcl").startsWith("#") && (testPass.get("hcl").length() == 7)){char[] charData = new char[6];testPass.get("hcl").getChars(1,7,charData,0);int validChar = 0;for (char c : charData) {if (Character.isLetter(c)){String b = Character.toString(c);if (b.equals("a") || b.equals("b") || b.equals("c") || b.equals("d") || b.equals("e") || b.equals("f") ) {validChar++;}} else {validChar++;}}if (validChar == 6) {validCheck++;}}}if (testPass.containsKey("ecl")){String b = testPass.get("ecl");if ((b.equals("amb")) ^ (b.equals("blu")) ^ (b.equals("brn")) ^ (b.equals("gry")) ^ (b.equals("grn")) ^ (b.equals("hzl")) ^ (b.equals("oth"))) {validCheck++;}}if (testPass.containsKey("pid")){if ((testPass.get("pid").length() == 9)){try {int n = Integer.parseInt(testPass.get("pid"));} catch (NumberFormatException e) {}validCheck++;}}if (validCheck == 7) {return true;}return false;}}