some-advent-of-code: diff 4a6ea549 f4171cc8

Branch: master

Commit: 4a6ea5496fa17a14267679588cf517cd33bc21d8

Author: Nicholas Prado <nmprado@nzen.ws>
Date: Fri Dec 3 02:01:11 UTC 2021
Parent: f4171cc89f10cffc399b41553fe39c75fa0b51a8
Log message:

    checkin 20 07 moved intermediate work to year folder
    
    This does not resolve the second part of 2020 exercise seven, but
    I'm unlikely to return to it, so I might as well commit it.

    1: diff --git a/src/java/y2020/Exercise200702.java b/src/java/y2020/Exercise200702.java
    2: new file mode 100644
    3: index 0000000..1db4c49
    4: --- /dev/null
    5: +++ b/src/java/y2020/Exercise200702.java
    6: @@ -0,0 +1,152 @@
    7: +
    8: +import java.io.IOException;
    9: +import java.nio.file.*;
   10: +import java.util.*;
   11: +import java.util.regex.*;;
   12: +
   13: +public class Exercise200702
   14: +{
   15: +
   16: +	public static void main( String args[] )
   17: +	{
   18: +		final String here = "e20071.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: +		// gather
   39: +		Map<String, Collection<String>> reverseAdjacency = new HashMap<>();
   40: +		for ( String line : fileLines )
   41: +		{
   42: +			if ( line.isEmpty() || line.contains( "no other" ) )
   43: +				continue;
   44: +			int firstBagsInd = line.indexOf( "bags" );
   45: +			String container = line.substring( 0, firstBagsInd -1 );
   46: +// System.out.println( here +"is "+ container );
   47: +			Collection<String> inside = containedIn( line.substring(
   48: +					firstBagsInd +"bags contain ".length() ) );
   49: +			for ( String inner : inside )
   50: +			{
   51: +				if ( ! reverseAdjacency.containsKey( inner ) )
   52: +					reverseAdjacency.put( inner, new HashSet<String>() );
   53: +				reverseAdjacency.get( inner ).add( container );
   54: +			}
   55: +		}
   56: +/* everything
   57: +for ( String inner : reverseAdjacency.keySet() )
   58: +{
   59: +	System.out.print( inner +";\t" );
   60: +	for ( String has : reverseAdjacency.get( inner ) )
   61: +		System.out.print( has +", " );
   62: +	System.out.println();
   63: +}
   64: +	System.out.println();
   65: +*/
   66: +		// trace adjacency
   67: +		Queue<String> toCheck = new LinkedList<>();
   68: +		int target = 0;
   69: +		Collection<String> containsGold = reverseAdjacency.get( "shiny gold" );
   70: +		if ( containsGold == null )
   71: +			throw new RuntimeException( here +"gold not contained by anything" );
   72: +// else
   73: +// System.out.println( "gold in "+ containsGold.size() );
   74: +		Set<String> outermost = new TreeSet<>();
   75: +		outermost.addAll( containsGold );
   76: +		toCheck.addAll( containsGold );
   77: +		while ( ! toCheck.isEmpty() )
   78: +		{
   79: +			String bag = toCheck.poll();
   80: +			if ( bag == null )
   81: +				continue;
   82: +			outermost.add( bag );
   83: +			Collection<String> containedBy = reverseAdjacency.get( bag );
   84: +// System.out.println( "huh "+ bag +" in "+ containedBy );
   85: +			if ( containedBy == null )
   86: +				continue;
   87: +
   88: +/*
   89: +System.out.print( bag +" contained by: "+ containedBy.size() +" " );
   90: +for ( String inner : containedBy )
   91: + System.out.print( "_"+ inner );
   92: +System.out.println( "; left-"+ toCheck.size() );
   93: +*/
   94: +
   95: +			toCheck.addAll( containedBy );
   96: +		}
   97: +		System.out.println( here +"input summed to "+ outermost.size() );
   98: +	}
   99: +
  100: +
  101: +	/** expecting '\d+ value bag(s), ...' and not given a 'no other' line. */
  102: +	private static Collection<String> containedIn(
  103: +			String list
  104: +	) {
  105: +// System.out.print( "\trest is -"+ list );
  106: +		Collection<String> inside = new LinkedList<>();
  107: +		for (
  108: +				int ind = 0;
  109: +				ind < list.length() && ind >= 0;
  110: +				ind += 1
  111: +		) {
  112: +			// number
  113: +			ind = list.indexOf( ' ', ind ) +1;
  114: +			// color
  115: +			int end = list.indexOf( "bag", ind );
  116: +			String color = list.substring( ind, end -1 );
  117: +			inside.add( color );
  118: +			ind = list.indexOf( ", ", end ) +2;
  119: +//System.out.println( "color "+ color +"; pointing at "+ ind +" is_"+ list.charAt( ind ) );
  120: +			if ( ind == 1 )
  121: +				break; // added 2 above
  122: +			
  123: +		}
  124: +		/*
  125: +		light red bags contain 1 bright white bag, 2 muted yellow bags.
  126: +		bright white bags contain 1 shiny gold bag.
  127: +		muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
  128: +		*/
  129: +		return inside;
  130: +	}
  131: +}
  132: +
  133: +
  134: +
  135: +
  136: +
  137: +
  138: +
  139: +
  140: +
  141: +
  142: +
  143: +
  144: +
  145: +
  146: +
  147: +
  148: +
  149: +
  150: +
  151: +
  152: +
  153: +
  154: +
  155: +
  156: +
  157: +
  158: +
  159: diff --git a/src/res/20_07_example.txt b/src/res/y2020/20_07_example.txt
  160: similarity index 100%
  161: rename from src/res/20_07_example.txt
  162: rename to src/res/y2020/20_07_example.txt
  163: diff --git a/src/res/20_07_input.txt b/src/res/y2020/20_07_input.txt
  164: similarity index 100%
  165: rename from src/res/20_07_input.txt
  166: rename to src/res/y2020/20_07_input.txt

Generated by git2html.