r/adventofcode • • Dec 04 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 04 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- 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

1.2k comments sorted by

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) {
//inches
int suffixLocation = testPass.get("hgt").indexOf("in");
int value = Integer.parseInt(testPass.get("hgt").substring(0, suffixLocation));
if ((value <= 76) && (value >= 59)) {
validCheck++;
}
} else {
//centimeter
int 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;
}

}

3

u/1vader Dec 04 '20

Your code formatting is a bit messed up. To properly format your code on Reddit you can use the "Code Block" button in the visual editor or simply indent it with four spaces in the markdown editor.

Although, at that length, you should also start to consider moving it to an external page and just linking it here. The Wiki has some pages for that.> Your code formatting is a bit messed up. To properly format your code on Reddit you can use the "Code Block" button in the visual editor or simply indent it with four spaces in the markdown editor.Although, at that length, you should also start to consider moving it to an external page and just linking it here. The Wiki has some pages for that.

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!