some-advent-of-code: 590a4c450caeeba565e1bbd2a6315ad7c6e560df

     1: 
     2: import java.io.IOException;
     3: import java.nio.file.Files;
     4: import java.nio.file.InvalidPathException;
     5: import java.nio.file.Path;
     6: import java.nio.file.Paths;
     7: import java.util.LinkedList;
     8: import java.util.List;
     9: import java.util.Map;
    10: import java.util.TreeMap;
    11: 
    12: public class Exercise19021
    13: {
    14: 
    15: 	public static void main( String args[] )
    16: 	{
    17: 		final String here = "e19021.m ";
    18: 		if ( args.length < 1 )
    19: 		{
    20: 			throw new RuntimeException( here +"add a filename argument" );
    21: 		}
    22: 		String userSaysFile = args[ 0 ];
    23: 		List<String> fileLines = new LinkedList<>();
    24: 		try
    25: 		{
    26: 			Path where = Paths.get( userSaysFile );
    27: 			fileLines = Files.readAllLines( where );
    28: 		}
    29: 		catch ( IOException | InvalidPathException ie )
    30: 		{
    31: 			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
    32: 			return;
    33: 		}
    34: 		/*
    35: 		- interpretation of spec -
    36: 		file is single line csv of a recurring format with four relevant indicies:
    37: 		{ opCode, left operand, right operand, target index }
    38: 		opcode 99 is halt
    39: 		opcode 1 is memory[ target ] = left + right
    40: 		opcode 2 is memory[ target ] = left * right
    41: 		after that's done, skip to the next opcode, which is to say, by four
    42: 		*/
    43: 		int initialInd = 0, loInd = initialInd +1, roInd = loInd +1,
    44: 				tInd = roInd +1, nextInd = tInd +1;
    45: 		String times = " * ", plus = " + ", operator = times;
    46: 		for ( String line : fileLines )
    47: 		{
    48: 			int pcounter = initialInd;
    49: 			String[] memory = line.split( "," );
    50: 			System.out.println( "\n\n  another run" );
    51: 			if ( memory.length < 50 )
    52: 			{
    53: 				for ( int indP = 0; indP < memory.length; indP += 1 )
    54: 				{
    55: 					System.out.print( memory[ indP ] +", " );
    56: 					if ( (indP % 4) == 3 )
    57: 						System.out.println();
    58: 				}
    59: 			}
    60: 			System.out.println( "- - -" );
    61: 			int sentinel = 1_000_000_000;
    62: 			int leftAddress = -1, rightAddress = -1, targetAddress = -1,
    63: 					result = -1, leftOperand = -1, rightOperand = -1;
    64: 			String opCode;
    65: 			while ( sentinel > 0 && pcounter < memory.length )
    66: 			{
    67: 				sentinel -= 1;
    68: 				opCode = memory[ pcounter ];
    69: 				if ( opCode.equals( "99" ) )
    70: 				{
    71: 					System.out.println( here +"halting" );
    72: 					break;
    73: 				}
    74: 				leftAddress = Integer.parseInt( memory[ pcounter + loInd ] );
    75: 				rightAddress = Integer.parseInt( memory[ pcounter + roInd ] );
    76: 				targetAddress = Integer.parseInt( memory[ pcounter + tInd ] );
    77: 				if ( leftAddress <= memory.length )
    78: 					leftOperand = Integer.parseInt( memory[ leftAddress ] );
    79: 				else
    80: 					System.out.println( here +"left address outside at "+ leftAddress );
    81: 				if ( rightAddress <= memory.length )
    82: 					rightOperand = Integer.parseInt( memory[ rightAddress ] );
    83: 				else
    84: 					System.out.println( here +"right address outside at "+ rightAddress );
    85: 				if ( opCode.equals( "1" ) )
    86: 				{
    87: 					result = leftOperand + rightOperand;
    88: 					operator = plus;
    89: 				}
    90: 				else if ( opCode.equals( "2" ) )
    91: 				{
    92: 					result = leftOperand * rightOperand;
    93: 					operator = times;
    94: 				}
    95: 				else
    96: 				{
    97: 					System.out.println( here +"unrecognized op code "+ opCode );
    98: 				}
    99: 				if ( targetAddress >= memory.length )
   100: 				{
   101: 					System.out.println( " "+ here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
   102: 							+ rightOperand +"@"+ rightAddress +" outside to "+ targetAddress );
   103: 				}
   104: 				else
   105: 				{
   106: 					System.out.println( here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
   107: 							+ rightOperand +"@"+ rightAddress +" to "
   108: 							+ targetAddress +" ["+ memory[ targetAddress ] +"]" );
   109: 					memory[ targetAddress ] = Integer.valueOf( result ).toString();
   110: 				}
   111: 				pcounter += nextInd;
   112: 			}
   113: 			
   114: 			System.out.println( "\t"+ here +"first opcode is "+ memory[ 0 ] );
   115: 			if ( memory.length < 50 )
   116: 			{
   117: 				for ( int indP = 0; indP < memory.length; indP += 1 )
   118: 				{
   119: 					System.out.print( memory[ indP ] +", " );
   120: 					if ( (indP % 4) == 3 )
   121: 						System.out.println();
   122: 				}
   123: 			}
   124: 		}
   125: 	}
   126: }
   127: 
   128: 
   129: 
   130: 
   131: 
   132: 
   133: 
   134: 
   135: 
   136: 
   137: 
   138: 
   139: 
   140: 
   141: 
   142: 
   143: 
   144: 
   145: 
   146: 
   147: 
   148: 
   149: 
   150: 
   151: 
   152: 
   153: 

Generated by git2html.