some-advent-of-code: 5974bdd0ce566260dfee57f9eb4025f7160cbde1

     1: 
     2: import java.io.IOException;
     3: import java.nio.file.*;
     4: import java.util.*;
     5: 
     6: public class Exercise210501
     7: {
     8: 
     9: 	public static void main(
    10: 			String args[]
    11: 	) {
    12: 		final String here = "e210501.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 Exercise210501().findVents( fileLines );
    41: 	}
    42: 
    43: 
    44: 	private void findVents(
    45: 			List<String> fileLines
    46: 	) {
    47: 	    boolean testing = true;
    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: 			{
    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: 			}
    66: 			if ( testing )
    67: 			    System.out.println( "\t"+ computedLine.direction.name() );
    68: 		}
    69: 		int totalIntersections = 0;
    70: 		for ( Integer linesThere : point_intersections.values() )
    71: 		    if ( linesThere > 1 )
    72: 		        totalIntersections++;
    73: 		System.out.println( "\t"+ totalIntersections );
    74: 	}
    75: 
    76: 
    77:     private Line lineAsPointsAndDirection(
    78:             String line
    79:     ) {
    80:         int firstInd = 0, secondInd = firstInd +1;
    81:         int xxInd = 0, yyInd = xxInd +1;
    82:         String comma = ",";
    83:         String[] barePoints = line.split( " -> " );
    84: 
    85:         String[] xyFirst = barePoints[ firstInd ].split( comma );
    86:         int xxFirst = Integer.parseInt( xyFirst[ xxInd ] );
    87:         int yyFirst = Integer.parseInt( xyFirst[ yyInd ] );
    88: 
    89:         String[] xySecond = barePoints[ secondInd ].split( comma );
    90:         int xxSecond = Integer.parseInt( xySecond[ xxInd ] );
    91:         int yySecond = Integer.parseInt( xySecond[ yyInd ] );
    92: 
    93:         Line computedLine = new Line();
    94:         Direction direction;
    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: 
   129:         return computedLine;
   130:     }
   131: 
   132:     enum Direction { horizontal, vertical, diagonal };
   133: 
   134:     class Line
   135:     {
   136:         Direction direction;
   137:         String[] points = {};
   138:     }
   139: 
   140: }
   141: 
   142: 
   143: 

Generated by git2html.