some-advent-of-code: 5884053af175328318621295b05fe5dbe5b8f574
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 Exercise200501
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: int validCount = 0, id = 0;
42: for ( String instruction : fileLines )
43: {
44: id = seatId( instruction );
45: if ( id > validCount )
46: validCount = id;
47: }
48: System.out.println( here +"input has "+ validCount +" valid trees" );
49: }
50:
51:
52: private static int seatId(
53: String instructions
54: ) {
55: final String here = "e20051.m ";
56: String rowInstructions = instructions.substring( 0, instructions.length() -3 );
57: String columnInstructions = instructions.substring( instructions.length() -3 );
58: int row = 0, lower = 0, upper = 127;
59: for ( int ind = 0; ind < rowInstructions.length(); ind++ )
60: {
61: int range = upper - lower;
62: int half = (int)Math.round( range / 2F );
63: if ( rowInstructions.charAt( ind ) == 'F' )
64: upper -= half;
65: else
66: lower += half;
67: }
68: row = lower;
69: int column = 0;
70: lower = 0; upper = 7;
71: for ( int ind = 0; ind < columnInstructions.length(); ind++ )
72: {
73: int range = upper - lower;
74: int half = (int)Math.round( range / 2F );
75: if ( columnInstructions.charAt( ind ) == 'L' )
76: upper -= half;
77: else
78: lower += half;
79: }
80: column = lower;
81: return row * 8 + column;
82: }
83:
84:
85:
86: }
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
Generated by git2html.