<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
import java.io.IOException;
import java.nio.file.*;
import java.util.*;

public class Exercise210202
{

	public static void main(
			String args[]
	) {
		final String here = "e210202.m ";
		if ( args.length &lt; 1 )
		{
			throw new RuntimeException( here +"add a filename argument" );
		}
		String userSaysFile = args[ 0 ];
		List&lt;String&gt; fileLines = new LinkedList&lt;&gt;();
		try
		{
			Path where = Paths.get( userSaysFile );
			fileLines = Files.readAllLines( where );
		}
		catch ( IOException | InvalidPathException ie )
		{
			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
			return;
		}
		/*
		- interpretation of spec -
		*/
		Exercise210202.findFinalDistance( fileLines );
	}


	private static void findFinalDistance(
			List&lt;String&gt; fileLines
	) {
		final int dirInd = 0, valInd = dirInd +1;
		int horizontalPosition = 0, depth = 0, aim = 0;
		for ( String line : fileLines )
		{
			if ( line.isEmpty() )
				continue;
			String[] linePieces = line.split( " " );
			int magnitude = Integer.parseInt( linePieces[ valInd ] );
			switch ( linePieces[ dirInd ] )
			{
			case "forward" :
				horizontalPosition += magnitude;
				depth += aim * magnitude;
				break;
			case "down" :
				aim += magnitude;
				break;
			case "up" :
				aim -= magnitude;
				break;
			default :
				continue;
			}
		}
		System.out.println( depth * horizontalPosition );
	}


}



























</pre></body></html>