Author: Nicholas Prado <nmprado@nzen.ws>
Date: Sun Dec 6 04:10:52 UTC 2020
Parent: e9f22b988f3a5d4ed19de92d9bf02906d1bde865
Log message:
feat 200502 set for missing As that seemed to be the simplest way to check in both directions.
1: diff --git a/src/java/Exercise200502.java b/src/java/Exercise200502.java 2: new file mode 100644 3: index 0000000..aa297cb 4: --- /dev/null 5: +++ b/src/java/Exercise200502.java 6: @@ -0,0 +1,128 @@ 7: + 8: +import java.awt.Point; 9: +import java.io.IOException; 10: +import java.nio.file.Files; 11: +import java.nio.file.InvalidPathException; 12: +import java.nio.file.Path; 13: +import java.nio.file.Paths; 14: +import java.util.HashSet; 15: +import java.util.LinkedList; 16: +import java.util.List; 17: +import java.util.Map; 18: +import java.util.Set; 19: +import java.util.TreeMap; 20: + 21: +public class Exercise200502 22: +{ 23: + private static final char tree = '#', ground = '.'; 24: + 25: + public static void main( String args[] ) 26: + { 27: + final String here = "e20011.m "; 28: + if ( args.length < 1 ) 29: + { 30: + throw new RuntimeException( here +"add a filename argument" ); 31: + } 32: + String userSaysFile = args[ 0 ]; 33: + List<String> fileLines = new LinkedList<>(); 34: + try 35: + { 36: + Path where = Paths.get( userSaysFile ); 37: + fileLines = Files.readAllLines( where ); 38: + } 39: + catch ( IOException | InvalidPathException ie ) 40: + { 41: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie ); 42: + return; 43: + } 44: + /* 45: + - interpretation of spec - 46: + */ 47: + Set<Integer> missingSeats = new HashSet<>(); 48: + for ( int rInd = 0; rInd < 128; rInd++ ) 49: + { 50: + for ( int cInd = 0; cInd < 8; cInd++ ) 51: + missingSeats.add( rInd * 8 + cInd ); 52: + } 53: + int validCount = 0, id = 0; 54: + for ( String instruction : fileLines ) 55: + { 56: + id = seatId( instruction ); 57: + missingSeats.remove( id ); 58: + } 59: + for ( Integer missing : missingSeats ) 60: + { 61: + if ( ! missingSeats.contains( missing +8 ) 62: + && ! missingSeats.contains( missing -8 ) ) 63: + { 64: + validCount = missing; 65: + System.out.println( here +"maybe "+ validCount ); 66: + // break; 67: + } 68: + } 69: + System.out.println( here +"input has "+ validCount +" valid trees" ); 70: + } 71: + 72: + 73: + private static int seatId( 74: + String instructions 75: + ) { 76: + final String here = "e20051.m "; 77: + String rowInstructions = instructions.substring( 0, instructions.length() -3 ); 78: + String columnInstructions = instructions.substring( instructions.length() -3 ); 79: + int row = 0, lower = 0, upper = 127; 80: + for ( int ind = 0; ind < rowInstructions.length(); ind++ ) 81: + { 82: + int range = upper - lower; 83: + int half = (int)Math.round( range / 2F ); 84: + if ( rowInstructions.charAt( ind ) == 'F' ) 85: + upper -= half; 86: + else 87: + lower += half; 88: + } 89: + row = lower; 90: + int column = 0; 91: + lower = 0; upper = 7; 92: + for ( int ind = 0; ind < columnInstructions.length(); ind++ ) 93: + { 94: + int range = upper - lower; 95: + int half = (int)Math.round( range / 2F ); 96: + if ( columnInstructions.charAt( ind ) == 'L' ) 97: + upper -= half; 98: + else 99: + lower += half; 100: + } 101: + column = lower; 102: + return row * 8 + column; 103: + } 104: + 105: + 106: + 107: +} 108: + 109: + 110: + 111: + 112: + 113: + 114: + 115: + 116: + 117: + 118: + 119: + 120: + 121: + 122: + 123: + 124: + 125: + 126: + 127: + 128: + 129: + 130: + 131: + 132: + 133: + 134: +