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

1

u/Imnewinhere Dec 04 '20

Java solution

I coded in Java, works for me, I would appreciate someone looking over it, to identify mistakes. If you use it remember to change your references!

package mainpkg;//<-- CHANGE YOUR PACKAGE

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

public class main {

public static void main(String\[\] args) throws IOException {
	int result = 0;
	String filePath = "src/mainpkg/pps.txt";//<-- CHANGE TO YOUR PATH
	String contents = null;

	try {

		contents = new String(Files.readAllBytes(Paths.get(filePath)));

	} catch (IOException e) {

		e.printStackTrace();

	}
	String\[\] pp = contents.split("\\\\r?\\\\n\\\\r?\\\\n");

	for (int i = 0; i < pp.length; i++) {

		String\[\] form = pp\[i\].split("\\\\r?\\\\n");

		pp\[i\] = "";

		for (int c = 0; c < form.length; c++) {

pp[i] = pp[i] + " " + form[c];

		}
		if (pp\[i\].contains("ecl:") && pp\[i\].contains("pid:") && pp\[i\].contains("byr:") && pp\[i\].contains("iyr:")

&& pp[i].contains("eyr:") && pp[i].contains("hgt:") && pp[i].contains("hcl:")) {

boolean byr = false;

boolean iyr = false;

boolean eyr = false;

boolean hgt = false;

boolean hcl = false;

boolean ecl = false;

boolean pid = false;

pp[i] = pp[i].substring(1);

//System.out.println(pp[i]);

String[] fields = pp[i].split(" ");

for (int d = 0; d < fields.length; d++) {

if (fields[d].contains("byr") && Integer.parseInt(fields[d].substring(4)) >= 1920

&& Integer.parseInt(fields[d].substring(4)) <= 2002)

byr = true;

else if (fields[d].contains("iyr") && (Integer.parseInt(fields[d].substring(4)) >= 2010

&& Integer.parseInt(fields[d].substring(4)) <= 2020))

iyr = true;

else if (fields[d].contains("eyr") && Integer.parseInt(fields[d].substring(4)) >= 2020

&& Integer.parseInt(fields[d].substring(4)) <= 2030)

eyr = true;

else if (fields[d].contains("hgt")) {

try {

if (fields[d].contains("in") && !Character.isDigit(fields[d].charAt(7)) && Integer.parseInt(fields[d].substring(4, 6)) >= 59

&& Integer.parseInt(fields[d].substring(4, 6)) <= 76)

hgt = true;

else if (fields[d].contains("cm") && Integer.parseInt(fields[d].substring(4, 7)) >= 150

&& Integer.parseInt(fields[d].substring(4, 7)) <= 193) {

hgt = true;

}

} catch (NumberFormatException e) {

// e.printStackTrace();

}

} else if (fields[d].contains("hcl") && fields[d].charAt(4) == '#'

&& fields[d].substring(5, 11).matches("-?[0-9a-f]+"))

hcl = true;

else if (fields[d].contains("ecl") && (fields[d].substring(4, 7).matches("blu")

|| fields[d].substring(4, 7).matches("brn") || fields[d].substring(4, 7).matches("gry")

|| fields[d].substring(4, 7).matches("grn") || fields[d].substring(4, 7).matches("hzl")

|| fields[d].substring(4, 7).matches("oth") || fields[d].substring(4, 7).matches("amb")))

ecl = true;

else if (fields[d].contains("pid")&&fields[d].length()==13&&fields[d].substring(4).matches("-?[0-9]+"))

pid = true;

}

if (byr && iyr && eyr && hgt && hcl && ecl && pid)

result++;

		}
	}
	System.out.println("Done");

	System.out.println(result);

}

}

1

u/daggerdragon Dec 04 '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?

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!