some-advent-of-code: c2f75eaaf0528bb79b38a3c2e3f1577703f0e994

     1: 
     2: import java.io.IOException;
     3: import java.nio.file.*;
     4: import java.util.*;
     5: 
     6: public class Exercise210201
     7: {
     8: 
     9: 	public static void main(
    10: 			String args[]
    11: 	) {
    12: 		final String here = "e210201.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: 		*/
    32: 		Exercise210201.findFinalDistance( fileLines );
    33: 	}
    34: 
    35: 
    36: 	private static void findFinalDistance(
    37: 			List<String> fileLines
    38: 	) {
    39: 		final int dirInd = 0, valInd = dirInd +1;
    40: 		int horizontalPosition = 0, depth = 0;
    41: 		for ( String line : fileLines )
    42: 		{
    43: 			if ( line.isEmpty() )
    44: 				continue;
    45: 			String[] linePieces = line.split( " " );
    46: 			int magnitude = Integer.parseInt( linePieces[ valInd ] );
    47: 			switch ( linePieces[ dirInd ] )
    48: 			{
    49: 			case "forward" :
    50: 				horizontalPosition += magnitude;
    51: 				break;
    52: 			case "down" :
    53: 				depth += magnitude;
    54: 				break;
    55: 			case "up" :
    56: 				depth -= magnitude;
    57: 				break;
    58: 			default :
    59: 				continue;
    60: 			}
    61: 		}
    62: 		System.out.println( depth * horizontalPosition );
    63: 	}
    64: 
    65: 
    66: }
    67: 
    68: 
    69: 
    70: 
    71: 
    72: 
    73: 
    74: 
    75: 
    76: 
    77: 
    78: 
    79: 
    80: 
    81: 
    82: 
    83: 
    84: 
    85: 
    86: 
    87: 
    88: 
    89: 
    90: 
    91: 
    92: 
    93: 

Generated by git2html.