some-advent-of-code: diff 78868be7 dbcceed9

Branch: master

Commit: 78868be76e1cdca9df3880c7664c81538255d3bc

Author: Nicholas Prado <nmprado@nzen.ws>
Date: Sat Dec 4 05:30:04 UTC 2021
Parent: dbcceed90ae0239ad9451d837cae064a967b4cdc
Log message:

    feat 21 03.2 stepwise sum and discard

    1: diff --git a/src/java/y2021/Exercise210302.java b/src/java/y2021/Exercise210302.java
    2: new file mode 100644
    3: index 0000000..cc6f3e1
    4: --- /dev/null
    5: +++ b/src/java/y2021/Exercise210302.java
    6: @@ -0,0 +1,165 @@
    7: +
    8: +import java.io.IOException;
    9: +import java.nio.file.*;
   10: +import java.util.*;
   11: +
   12: +public class Exercise210302
   13: +{
   14: +
   15: +	public static void main(
   16: +			String args[]
   17: +	) {
   18: +		final String here = "e210302.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: +		sum the bits individually, discard and resum as necessary
   38: +		*/
   39: +		Exercise210302.interpretDiagnostic( fileLines );
   40: +	}
   41: +
   42: +
   43: +	private static void interpretDiagnostic(
   44: +			List<String> fileLines
   45: +	) {
   46: +		int ones = 0, nils = 0;
   47: +		int oxygenRating = 0, carbonRating = 0;
   48: +		List<String> previousCandidates = new ArrayList<>( fileLines.size() );
   49: +		previousCandidates.addAll( fileLines );
   50: +		List<String> currentCandidates = new LinkedList<>();
   51: +		int len = fileLines.get( 0 ).length();
   52: +
   53: +		// oxygen
   54: +		for ( int charInd = 0; charInd < len; charInd++ )
   55: +		{
   56: +			ones = 0;
   57: +			nils = 0;
   58: +			for ( String line : previousCandidates )
   59: +			{
   60: +				if ( line.charAt( charInd ) == '1' )
   61: +					ones += 1;
   62: +				else // == 0
   63: +					nils += 1;
   64: +			}
   65: +
   66: +			char toMatch;
   67: +			if ( ones > nils )
   68: +				toMatch = '1';
   69: +			else if ( ones < nils )
   70: +				toMatch = '0';
   71: +			else
   72: +				toMatch = '='; // for clarity when comparing == below
   73: +			for ( String candidate : previousCandidates )
   74: +			{
   75: +				if ( candidate.charAt( charInd ) == toMatch
   76: +						|| ( ones == nils
   77: +							&& candidate.charAt( charInd ) == '1' ) )
   78: +					currentCandidates.add( candidate );
   79: +			}
   80: +			if ( currentCandidates.size() == 1 )
   81: +				break;
   82: +			previousCandidates.clear();
   83: +			previousCandidates.addAll( currentCandidates );
   84: +			currentCandidates.clear();
   85: +		}
   86: +		oxygenRating = Exercise210302.asInt( currentCandidates.get( 0 ) );
   87: +
   88: +		// carbon
   89: +		previousCandidates.clear();
   90: +		previousCandidates.addAll( fileLines );
   91: +		currentCandidates.clear();
   92: +		for ( int charInd = 0; charInd < len; charInd++ )
   93: +		{
   94: +			ones = 0;
   95: +			nils = 0;
   96: +			for ( String line : previousCandidates )
   97: +			{
   98: +				if ( line.charAt( charInd ) == '1' )
   99: +					ones += 1;
  100: +				else // == 0
  101: +					nils += 1;
  102: +			}
  103: +
  104: +			char toMatch;
  105: +			if ( ones < nils )
  106: +				toMatch = '1';
  107: +			else if ( ones > nils )
  108: +				toMatch = '0';
  109: +			else
  110: +				toMatch = '='; // for clarity when comparing == below
  111: +			for ( String candidate : previousCandidates )
  112: +			{
  113: +				if ( candidate.charAt( charInd ) == toMatch
  114: +						|| ( ones == nils
  115: +							&& candidate.charAt( charInd ) == '0' ) )
  116: +					currentCandidates.add( candidate );
  117: +			}
  118: +			if ( currentCandidates.size() == 1 )
  119: +				break;
  120: +			previousCandidates.clear();
  121: +			previousCandidates.addAll( currentCandidates );
  122: +			currentCandidates.clear();
  123: +		}
  124: +		carbonRating = Exercise210302.asInt( currentCandidates.get( 0 ) );
  125: +
  126: +		System.out.println( "\to"+ oxygenRating +" c"+ carbonRating +" t"+ oxygenRating * carbonRating );
  127: +	}
  128: +
  129: +
  130: +	private static int asInt(
  131: +			String bitChars
  132: +	) {
  133: +		int value = 0, multiplier = 1;
  134: +		for ( int charInd = bitChars.length() -1; charInd >= 0; charInd-- )
  135: +		{
  136: +			if ( bitChars.charAt( charInd ) == '1' )
  137: +				value += multiplier;
  138: +			multiplier *= 2;
  139: +		}
  140: +		return value;
  141: +	}
  142: +
  143: +
  144: +}
  145: +
  146: +
  147: +
  148: +
  149: +
  150: +
  151: +
  152: +
  153: +
  154: +
  155: +
  156: +
  157: +
  158: +
  159: +
  160: +
  161: +
  162: +
  163: +
  164: +
  165: +
  166: +
  167: +
  168: +
  169: +
  170: +
  171: +
  172: diff --git a/src/res/y2021/21_03_solutions.txt b/src/res/y2021/21_03_solutions.txt
  173: new file mode 100644
  174: index 0000000..15f4557
  175: --- /dev/null
  176: +++ b/src/res/y2021/21_03_solutions.txt
  177: @@ -0,0 +1,6 @@
  178: +
  179: +example 1 : 198
  180: +puzzle 1 : 1071734
  181: +
  182: +example 2 : 230
  183: +puzzle 2 : 6124992

Generated by git2html.