some-advent-of-code: 0da389a5b8e63c663d8a87ace881916025afe081

     1: 
     2: import java.io.IOException;
     3: import java.nio.file.*;
     4: import java.util.*;
     5: 
     6: public class Exercise210202
     7: {
     8: 
     9: 	public static void main(
    10: 			String args[]
    11: 	) {
    12: 		final String here = "e210202.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: 		Exercise210202.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, aim = 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: 				depth += aim * magnitude;
    52: 				break;
    53: 			case "down" :
    54: 				aim += magnitude;
    55: 				break;
    56: 			case "up" :
    57: 				aim -= magnitude;
    58: 				break;
    59: 			default :
    60: 				continue;
    61: 			}
    62: 		}
    63: 		System.out.println( depth * horizontalPosition );
    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: 
    94: 

Generated by git2html.