# HG changeset patch # User Scoopta # Date 1607247884 28800 # Sun Dec 06 01:44:44 2020 -0800 # Node ID b77010af0557e6a4614970a5bcadc394d6beb7cf # Parent c1bef2865c95f26dac79337d8630e98fe950871b Day 5 diff --git a/src/ninja/scoopta/software/advent_of_code_12020/Day5.java b/src/ninja/scoopta/software/advent_of_code_12020/Day5.java new file mode 100644 --- /dev/null +++ b/src/ninja/scoopta/software/advent_of_code_12020/Day5.java @@ -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 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 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 lines) { + TreeSet 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; + } + } + } +}