some-advent-of-code: 673eca42a6f89ff304cadd48be034be4904d19ee
1:
2: import java.io.IOException;
3: import java.nio.file.*;
4: import java.util.*;
5:
6: public class Exercise210502
7: {
8:
9: public static void main(
10: String args[]
11: ) {
12: final String here = "e210502.m ";
13: if ( args.length < 1 )
14: {
15: throw new RuntimeException( here +"add a filename argument" );
16: }
17: String userSaysFile = args[ 0 ];
18: List<String> fileLines = new LinkedList<>();
19: try
20: {
21: Path where = Paths.get( userSaysFile );
22: fileLines = Files.readAllLines( where );
23: }
24: catch ( IOException | InvalidPathException ie )
25: {
26: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
27: return;
28: }
29: /*
30: - interpretation of spec -
31: for lines
32: parse as first and last points
33: if vertical or horizontal
34: save points
35: // diagonal are for later
36: for values in map
37: add if total > 1
38: print total
39: */
40: new Exercise210502().findVents( fileLines );
41: }
42:
43:
44: private void findVents(
45: List<String> fileLines
46: ) {
47: boolean testing = false;
48: Map<String, Integer> point_intersections = new HashMap<>();
49: for ( String line : fileLines )
50: {
51: if ( line.isEmpty() )
52: continue;
53: Line computedLine = lineAsPointsAndDirection( line );
54: //if ( computedLine.direction != Direction.diagonal )
55: //continue;
56: for ( String point : computedLine.points )
57: {
58: if ( testing )
59: System.out.print( point +" | " );
60: if ( point_intersections.containsKey( point ) )
61: point_intersections.put( point, point_intersections.get( point ) +1 );
62: else
63: point_intersections.put( point, 1 );
64: }
65: if ( testing )
66: System.out.println( "\t"+ computedLine.direction.name() );
67: }
68: int totalIntersections = 0;
69: for ( Integer linesThere : point_intersections.values() )
70: if ( linesThere > 1 )
71: totalIntersections++;
72: if ( testing )
73: showPoints( point_intersections, 10 );
74: System.out.println( "\ttotal "+ totalIntersections );
75: }
76:
77:
78: private Line lineAsPointsAndDirection(
79: String line
80: ) {
81: int firstInd = 0, secondInd = firstInd +1;
82: int xxInd = 0, yyInd = xxInd +1;
83: String comma = ",";
84: String[] barePoints = line.split( " -> " );
85:
86: String[] xyFirst = barePoints[ firstInd ].split( comma );
87: int xxFirst = Integer.parseInt( xyFirst[ xxInd ] );
88: int yyFirst = Integer.parseInt( xyFirst[ yyInd ] );
89:
90: String[] xySecond = barePoints[ secondInd ].split( comma );
91: int xxSecond = Integer.parseInt( xySecond[ xxInd ] );
92: int yySecond = Integer.parseInt( xySecond[ yyInd ] );
93:
94: Line computedLine = new Line();
95:
96: if ( xxFirst == xxSecond )
97: {
98: computedLine.direction = Direction.horizontal;
99: int yyGreater = yyFirst > yySecond ? yyFirst : yySecond;
100: int yyLesser = yyFirst < yySecond ? yyFirst : yySecond;
101: int distance = yyGreater - yyLesser;
102: computedLine.points = new String[ distance +1 ];
103: for ( int ind = 0; ind < distance +1; ind++ )
104: {
105: int yyVal = yyLesser + ind;
106: String point = xyFirst[ xxInd ] +","+ Integer.toString( yyVal );
107: computedLine.points[ ind ] = point;
108: }
109: }
110: else if ( yyFirst == yySecond )
111: {
112: computedLine.direction = Direction.vertical;
113: int xxGreater = xxFirst > xxSecond ? xxFirst : xxSecond;
114: int xxLesser = xxFirst < xxSecond ? xxFirst : xxSecond;
115: int distance = xxGreater - xxLesser;
116: computedLine.points = new String[ distance +1 ];
117: for ( int ind = 0; ind < distance +1; ind++ )
118: {
119: int xxVal = xxLesser + ind;
120: String point = Integer.toString( xxVal ) +","+ xyFirst[ yyInd ];
121: computedLine.points[ ind ] = point;
122: }
123: }
124: else
125: {
126: computedLine.direction = Direction.diagonal;
127: /*
128: is \ when slope is negative
129: if \ then increment both
130: if / then increment x and decrement y
131: */
132: boolean firstIsLeftmost = xxFirst < xxSecond;
133: boolean yyIncreases = ( firstIsLeftmost && yyFirst < yySecond )
134: || ( ! firstIsLeftmost && yyFirst > yySecond );
135: int xxDistance = firstIsLeftmost
136: ? ( xxSecond - xxFirst ) : ( xxFirst - xxSecond );
137: computedLine.points = new String[ xxDistance +1 ];
138: int xxLeft, yyLeft, xxRight, yyRight;
139: if ( firstIsLeftmost )
140: {
141: xxLeft = xxFirst;
142: yyLeft = yyFirst;
143: xxRight = xxSecond;
144: yyRight = yySecond;
145: }
146: else
147: {
148: xxLeft = xxSecond;
149: yyLeft = yySecond;
150: xxRight = xxFirst;
151: yyRight = yyFirst;
152: }
153: if ( yyIncreases )
154: {
155: for ( int ind = 0; ind < xxDistance +1; ind++ )
156: {
157: String point = Integer.toString( xxLeft + ind )
158: +","+ Integer.toString( yyLeft + ind );
159: computedLine.points[ ind ] = point;
160: //System.out.println( " "+ point );
161: }
162: }
163: else
164: {
165: for ( int ind = 0; ind < xxDistance +1; ind++ )
166: {
167: String point = Integer.toString( xxLeft + ind )
168: +","+ Integer.toString( yyLeft - ind );
169: computedLine.points[ ind ] = point;
170: //System.out.println( " "+ point );
171: }
172: }
173: }
174:
175: return computedLine;
176: }
177:
178:
179: private void showPoints(
180: Map<String, Integer> point_intersections, int squareMagnitude
181: ) {
182: String[][] board = new String[ squareMagnitude ][ squareMagnitude ];
183: for ( int xx = 0; xx < squareMagnitude; xx++ )
184: for ( int yy = 0; yy < squareMagnitude; yy++ )
185: board[ xx ][ yy ] = " ";
186:
187: int xxInd = 0, yyInd = xxInd +1;
188: String comma = ",";
189:
190: for ( String pointStr : point_intersections.keySet() )
191: {
192: String[] xyFirst = pointStr.split( comma );
193: int xxFirst = Integer.parseInt( xyFirst[ xxInd ] );
194: int yyFirst = Integer.parseInt( xyFirst[ yyInd ] );
195:
196: board[ xxFirst ][ yyFirst ] = Integer.toString( point_intersections.get( pointStr ) );
197: }
198:
199: System.out.print( "\n | " );
200: for ( int xx = 0; xx < squareMagnitude; xx++ )
201: System.out.print( xx +" " );
202: System.out.println();
203: for ( int xx = 0; xx < squareMagnitude; xx++ )
204: // for ( int xx = squareMagnitude -1; xx >= 0; xx-- )
205: {
206: System.out.print( xx +"|" );
207: for ( int yy = 0; yy < squareMagnitude; yy++ )
208: System.out.print( " "+ board[ yy ][ xx ] );
209: System.out.println();
210: }
211:
212: }
213:
214: enum Direction { horizontal, vertical, diagonal };
215:
216: class Line
217: {
218: Direction direction;
219: String[] points = {};
220: }
221:
222: }
223:
224:
225:
Generated by git2html.