some-advent-of-code: diff c8b425bd 57191301

Branch: master

Commit: c8b425bd2cee253b83d83acc5c1c263186ad40a8

Author: Nicholas Prado <nmprado@nzen.ws>
Date: Fri Dec 3 02:24:58 UTC 2021
Parent: 571913013f5a14082d0d1f8e025f91b50624c786
Log message:

    feat 21 02.2 aim

    1: diff --git a/src/java/y2021/Exercise210202.java b/src/java/y2021/Exercise210202.java
    2: new file mode 100644
    3: index 0000000..0da389a
    4: --- /dev/null
    5: +++ b/src/java/y2021/Exercise210202.java
    6: @@ -0,0 +1,94 @@
    7: +
    8: +import java.io.IOException;
    9: +import java.nio.file.*;
   10: +import java.util.*;
   11: +
   12: +public class Exercise210202
   13: +{
   14: +
   15: +	public static void main(
   16: +			String args[]
   17: +	) {
   18: +		final String here = "e210202.m ";
   19: +		if ( args.length < 1 )
   20: +		{
   21: +			throw new RuntimeException( here +"add a filename argument" );
   22: +		}
   23: +		String userSaysFile = args[ 0 ];
   24: +		List<String> fileLines = new LinkedList<>();
   25: +		try
   26: +		{
   27: +			Path where = Paths.get( userSaysFile );
   28: +			fileLines = Files.readAllLines( where );
   29: +		}
   30: +		catch ( IOException | InvalidPathException ie )
   31: +		{
   32: +			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
   33: +			return;
   34: +		}
   35: +		/*
   36: +		- interpretation of spec -
   37: +		*/
   38: +		Exercise210202.findFinalDistance( fileLines );
   39: +	}
   40: +
   41: +
   42: +	private static void findFinalDistance(
   43: +			List<String> fileLines
   44: +	) {
   45: +		final int dirInd = 0, valInd = dirInd +1;
   46: +		int horizontalPosition = 0, depth = 0, aim = 0;
   47: +		for ( String line : fileLines )
   48: +		{
   49: +			if ( line.isEmpty() )
   50: +				continue;
   51: +			String[] linePieces = line.split( " " );
   52: +			int magnitude = Integer.parseInt( linePieces[ valInd ] );
   53: +			switch ( linePieces[ dirInd ] )
   54: +			{
   55: +			case "forward" :
   56: +				horizontalPosition += magnitude;
   57: +				depth += aim * magnitude;
   58: +				break;
   59: +			case "down" :
   60: +				aim += magnitude;
   61: +				break;
   62: +			case "up" :
   63: +				aim -= magnitude;
   64: +				break;
   65: +			default :
   66: +				continue;
   67: +			}
   68: +		}
   69: +		System.out.println( depth * horizontalPosition );
   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: +
   95: +
   96: +
   97: +
   98: +
   99: +
  100: +

Generated by git2html.