Day 5
1 files changed, 90 insertions(+), 0 deletions(-)

A => src/ninja/scoopta/software/advent_of_code_12020/Day5.java
A => src/ninja/scoopta/software/advent_of_code_12020/Day5.java +90 -0
@@ 0,0 1,90 @@ 
+package ninja.scoopta.software.advent_of_code_12020;
+
+import java.util.*;
+import java.nio.file.*;
+import java.io.IOException;
+
+final class Day5 {
+	public static final void main(String[] args) {
+		try {
+			List<String> lines = Files.readAllLines(Paths.get("day5.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) {
+		int max = 0;
+		
+		for(String line : lines) {
+			int rowMin = 0;
+			int rowMax = 127;
+			int columnMin = 0;
+			int columnMax = 7;
+			
+			for(char chr : line.toCharArray()) {
+				switch(chr) {
+				case 'F':
+					rowMax = (rowMax - rowMin) / 2 + rowMin;
+					break;
+				case 'B':
+					rowMin += (rowMax - rowMin) / 2 + 1;
+					break;
+				case 'L':
+					columnMax = (columnMax - columnMin) / 2 + columnMin;
+					break;
+				case 'R':
+					columnMin += (columnMax - columnMin) / 2 + 1;
+					break;
+				}
+			}
+			max = Math.max(max, rowMax * 8 + columnMax);
+		}
+		System.out.println(max);
+	}
+	
+	private static final void pt2(List<String> lines) {
+		TreeSet<Integer> seats = new TreeSet<>();
+		int max = 0;
+		int min = Integer.MAX_VALUE;
+		
+		for(String line : lines) {
+			int rowMin = 0;
+			int rowMax = 127;
+			int columnMin = 0;
+			int columnMax = 7;
+			
+			for(char chr : line.toCharArray()) {
+				switch(chr) {
+				case 'F':
+					rowMax = (rowMax - rowMin) / 2 + rowMin;
+					break;
+				case 'B':
+					rowMin += (rowMax - rowMin) / 2 + 1;
+					break;
+				case 'L':
+					columnMax = (columnMax - columnMin) / 2 + columnMin;
+					break;
+				case 'R':
+					columnMin += (columnMax - columnMin) / 2 + 1;
+					break;
+				}
+			}
+			int seat = rowMax * 8 + columnMax;
+			seats.add(seat);
+			max = Math.max(max, seat);
+			min = Math.min(min, seat);
+		}
+		
+		for(int count = min; count < max; ++count) {
+			if(!seats.contains(count)) {
+				System.out.println(count);
+				return;
+			}
+		}
+	}
+}