some-advent-of-code: aa297cbb167d0d2eb970ad6db8eca3ac4441aadf
1:
2: import java.awt.Point;
3: import java.io.IOException;
4: import java.nio.file.Files;
5: import java.nio.file.InvalidPathException;
6: import java.nio.file.Path;
7: import java.nio.file.Paths;
8: import java.util.HashSet;
9: import java.util.LinkedList;
10: import java.util.List;
11: import java.util.Map;
12: import java.util.Set;
13: import java.util.TreeMap;
14:
15: public class Exercise200502
16: {
17: private static final char tree = '#', ground = '.';
18:
19: public static void main( String args[] )
20: {
21: final String here = "e20011.m ";
22: if ( args.length < 1 )
23: {
24: throw new RuntimeException( here +"add a filename argument" );
25: }
26: String userSaysFile = args[ 0 ];
27: List<String> fileLines = new LinkedList<>();
28: try
29: {
30: Path where = Paths.get( userSaysFile );
31: fileLines = Files.readAllLines( where );
32: }
33: catch ( IOException | InvalidPathException ie )
34: {
35: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
36: return;
37: }
38: /*
39: - interpretation of spec -
40: */
41: Set<Integer> missingSeats = new HashSet<>();
42: for ( int rInd = 0; rInd < 128; rInd++ )
43: {
44: for ( int cInd = 0; cInd < 8; cInd++ )
45: missingSeats.add( rInd * 8 + cInd );
46: }
47: int validCount = 0, id = 0;
48: for ( String instruction : fileLines )
49: {
50: id = seatId( instruction );
51: missingSeats.remove( id );
52: }
53: for ( Integer missing : missingSeats )
54: {
55: if ( ! missingSeats.contains( missing +8 )
56: && ! missingSeats.contains( missing -8 ) )
57: {
58: validCount = missing;
59: System.out.println( here +"maybe "+ validCount );
60: // break;
61: }
62: }
63: System.out.println( here +"input has "+ validCount +" valid trees" );
64: }
65:
66:
67: private static int seatId(
68: String instructions
69: ) {
70: final String here = "e20051.m ";
71: String rowInstructions = instructions.substring( 0, instructions.length() -3 );
72: String columnInstructions = instructions.substring( instructions.length() -3 );
73: int row = 0, lower = 0, upper = 127;
74: for ( int ind = 0; ind < rowInstructions.length(); ind++ )
75: {
76: int range = upper - lower;
77: int half = (int)Math.round( range / 2F );
78: if ( rowInstructions.charAt( ind ) == 'F' )
79: upper -= half;
80: else
81: lower += half;
82: }
83: row = lower;
84: int column = 0;
85: lower = 0; upper = 7;
86: for ( int ind = 0; ind < columnInstructions.length(); ind++ )
87: {
88: int range = upper - lower;
89: int half = (int)Math.round( range / 2F );
90: if ( columnInstructions.charAt( ind ) == 'L' )
91: upper -= half;
92: else
93: lower += half;
94: }
95: column = lower;
96: return row * 8 + column;
97: }
98:
99:
100:
101: }
102:
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:
Generated by git2html.