Day 4
1 files changed, 127 insertions(+), 0 deletions(-)

A => src/ninja/scoopta/software/advent_of_code_12020/Day4.java
A => src/ninja/scoopta/software/advent_of_code_12020/Day4.java +127 -0
@@ 0,0 1,127 @@ 
+package ninja.scoopta.software.advent_of_code_12020;
+
+import java.util.List;
+import java.nio.file.*;
+import java.io.IOException;
+
+final class Day4 {
+	public static final void main(String[] args) {
+		try {
+			List<String> lines = Files.readAllLines(Paths.get("day4.txt"));
+			System.out.println("Part 1");
+			pt1(lines);
+			System.out.println("Part 2");
+			pt2(lines);
+		} catch(IOException e) {
+			e.printStackTrace();
+		}
+	}
+	
+	private static final void pt1(List<String> lines) {
+		boolean byr, iyr, eyr, hgt, hcl, ecl, pid;
+		byr = iyr = eyr = hgt = hcl = ecl = pid = false;
+		int valid = 0;
+		
+		for(String line : lines) {
+			if(line.equals("")) {
+				if(byr && iyr && eyr && hgt && hcl && ecl && pid) {
+					++valid;
+				}
+				byr = iyr = eyr = hgt = hcl = ecl = pid = false;
+				continue;
+			}
+			byr = byr || line.contains("byr");
+			iyr = iyr || line.contains("iyr");
+			eyr = eyr || line.contains("eyr");
+			hgt = hgt || line.contains("hgt");
+			hcl = hcl || line.contains("hcl");
+			ecl = ecl || line.contains("ecl");
+			pid = pid || line.contains("pid");
+		}
+		if(byr && iyr && eyr && hgt && hcl && ecl && pid) {
+			++valid;
+		}
+		System.out.println(valid);
+	}
+	
+	private static final boolean pt2Check(String byr, String iyr, String eyr, String hgt, String hcl, String ecl, String pid) {
+		try {
+			int by = Integer.parseInt(byr);
+			int iy = Integer.parseInt(iyr);
+			int ey = Integer.parseInt(eyr);
+			boolean in = hgt.contains("in");
+			boolean cm = hgt.contains("cm");
+			int hg = Integer.parseInt(hgt.replace("in", "").replace("cm", ""));
+			
+			
+			boolean byrb = by >= 1920 && by <= 2002;
+			boolean iyrb = iy >= 2010 && iy <= 2020;
+			boolean eyrb = ey >= 2020 && ey <= 2030;
+			boolean hgtb = false;
+			if(in) {
+				hgtb = hg >= 59 && hg <= 76;
+			}
+			if(cm) {
+				hgtb = hg >= 150 && hg <= 193;
+			}
+			boolean hclb = hcl.matches("#[0-9a-fA-F]{6}");
+			boolean eclb = ecl.equals("amb") || ecl.equals("blu") || ecl.equals("brn") || ecl.equals("gry") || ecl.equals("grn") || ecl.equals("hzl") || ecl.equals("oth");
+			boolean pidb = pid.matches("[0-9]{9}");
+			
+			if(byrb && iyrb && eyrb && hgtb && hclb && eclb && pidb) {
+				return true;
+			}
+			return false;
+		} catch(NumberFormatException e) {
+			return false;
+		}
+	}
+	
+	private static final void pt2(List<String> lines) {
+		String byr, iyr, eyr, hgt, hcl, ecl, pid;
+		byr = iyr = eyr = hgt = hcl = ecl = pid = "";
+		int valid = 0;
+		
+		for(String line : lines) {
+			if(line.equals("")) {
+				if(pt2Check(byr, iyr, eyr, hgt, hcl, ecl, pid)) {
+					++valid;
+				}
+				byr = iyr = eyr = hgt = hcl = ecl = pid = "";
+				continue;
+			}
+			
+			String[] keys = line.split(" ");
+			for(String data : keys) {
+				String key = data.split(":")[0];
+				switch(key) {
+				case "byr":
+					byr = data.split(":")[1];
+					break;
+				case "iyr":
+					iyr = data.split(":")[1];
+					break;
+				case "eyr":
+					eyr = data.split(":")[1];
+					break;
+				case "hgt":
+					hgt = data.split(":")[1];
+					break;
+				case "hcl":
+					hcl = data.split(":")[1];
+					break;
+				case "ecl":
+					ecl = data.split(":")[1];
+					break;
+				case "pid":
+					pid = data.split(":")[1];
+					break;
+				}
+			}
+		}
+		if(pt2Check(byr, iyr, eyr, hgt, hcl, ecl, pid)) {
+			++valid;
+		}
+		System.out.println(valid);
+	}
+}