some-advent-of-code: diff 6de04381 ca42cf8d

Branch: master

Commit: 6de043814e0147bc4973abb052b554d545961ba6

Author: Nicholas Prado <nmprado@nzen.ws>
Date: Fri Dec 4 04:07:53 UTC 2020
Parent: ca42cf8d3069c13756a8e4dc16409e4a120fb582
Log message:

    feat 20 03 iterates over the original strings
    
    I invested in extracting the slope section because a) I misread
    the prompt and thought we had to iterate over all of them (though
    this was before I sat to type) and b) noticed a hint that we would
    need to iterate over different slopes.
    I am ambivalent that this was still managable today. Hopefully I'm
    out tomorrow.

    1: diff --git a/src/java/Exercise200301.java b/src/java/Exercise200301.java
    2: new file mode 100644
    3: index 0000000..f0a02e3
    4: --- /dev/null
    5: +++ b/src/java/Exercise200301.java
    6: @@ -0,0 +1,113 @@
    7: +
    8: +import java.awt.Point;
    9: +import java.io.IOException;
   10: +import java.nio.file.Files;
   11: +import java.nio.file.InvalidPathException;
   12: +import java.nio.file.Path;
   13: +import java.nio.file.Paths;
   14: +import java.util.HashSet;
   15: +import java.util.LinkedList;
   16: +import java.util.List;
   17: +import java.util.Map;
   18: +import java.util.Set;
   19: +import java.util.TreeMap;
   20: +
   21: +public class Exercise200301
   22: +{
   23: +	private static final char tree = '#', ground = '.';
   24: +
   25: +	public static void main( String args[] )
   26: +	{
   27: +		final String here = "e20011.m ";
   28: +		if ( args.length < 1 )
   29: +		{
   30: +			throw new RuntimeException( here +"add a filename argument" );
   31: +		}
   32: +		String userSaysFile = args[ 0 ];
   33: +		List<String> fileLines = new LinkedList<>();
   34: +		try
   35: +		{
   36: +			Path where = Paths.get( userSaysFile );
   37: +			fileLines = Files.readAllLines( where );
   38: +		}
   39: +		catch ( IOException | InvalidPathException ie )
   40: +		{
   41: +			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
   42: +			return;
   43: +		}
   44: +		/*
   45: +		- interpretation of spec -
   46: +		*/
   47: +		Point slope = new Point( 3, 1 );
   48: +		int validCount = treesInPath( fileLines, slope );
   49: +		System.out.println( here +"input has "+ validCount +" valid trees" );
   50: +	}
   51: +
   52: +
   53: +	private static int treesInPath(
   54: +			List<String> map,
   55: +			Point slope
   56: +	) {
   57: +		final String here = "e20031.m ";
   58: +		int trees = 0, times = 500;
   59: +		for ( Point ind = new Point( 0, 0 );
   60: +				ind.y < map.size() && times > 0;
   61: +				ind = nextPosition( map, ind, slope ) )
   62: +		{
   63: +			char collideWith = map.get( ind.y ).charAt( ind.x );
   64: +			if ( collideWith == tree )
   65: +			{
   66: +				trees += 1;			
   67: +	System.out.println( here +"tree at "+ ind );
   68: +			}
   69: +			times--;
   70: +		}
   71: +if ( times == 0 )
   72: +	System.out.println( here +"quit by count, " );
   73: +		return trees;
   74: +	}
   75: +
   76: +
   77: +	private static Point nextPosition(
   78: +			List<String> map,
   79: +			Point ind,
   80: +			Point slope
   81: +	) {
   82: +		int nextY = ind.y + slope.y;
   83: +		int nextX = ind.x + slope.x;
   84: +		if ( nextX >= map.get( 0 ).length() )
   85: +		{
   86: +			nextX = nextX - map.get( 0 ).length();
   87: +		}
   88: +		ind.setLocation( nextX, nextY );
   89: +		return ind;
   90: +	}
   91: +
   92: +}
   93: +
   94: +
   95: +
   96: +
   97: +
   98: +
   99: +
  100: +
  101: +
  102: +
  103: +
  104: +
  105: +
  106: +
  107: +
  108: +
  109: +
  110: +
  111: +
  112: +
  113: +
  114: +
  115: +
  116: +
  117: +
  118: +
  119: +
  120: diff --git a/src/java/Exercise200302.java b/src/java/Exercise200302.java
  121: new file mode 100644
  122: index 0000000..1b66c53
  123: --- /dev/null
  124: +++ b/src/java/Exercise200302.java
  125: @@ -0,0 +1,122 @@
  126: +
  127: +import java.awt.Point;
  128: +import java.io.IOException;
  129: +import java.nio.file.Files;
  130: +import java.nio.file.InvalidPathException;
  131: +import java.nio.file.Path;
  132: +import java.nio.file.Paths;
  133: +import java.util.HashSet;
  134: +import java.util.LinkedList;
  135: +import java.util.List;
  136: +import java.util.Map;
  137: +import java.util.Set;
  138: +import java.util.TreeMap;
  139: +
  140: +public class Exercise200302
  141: +{
  142: +	private static final char tree = '#', ground = '.';
  143: +
  144: +	public static void main( String args[] )
  145: +	{
  146: +		final String here = "e20011.m ";
  147: +		if ( args.length < 1 )
  148: +		{
  149: +			throw new RuntimeException( here +"add a filename argument" );
  150: +		}
  151: +		String userSaysFile = args[ 0 ];
  152: +		List<String> fileLines = new LinkedList<>();
  153: +		try
  154: +		{
  155: +			Path where = Paths.get( userSaysFile );
  156: +			fileLines = Files.readAllLines( where );
  157: +		}
  158: +		catch ( IOException | InvalidPathException ie )
  159: +		{
  160: +			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
  161: +			return;
  162: +		}
  163: +		/*
  164: +		- interpretation of spec -
  165: +		*/
  166: +		List<Point> directions = new LinkedList<>();
  167: +		directions.add( new Point( 1, 1 ) );
  168: +		directions.add( new Point( 3, 1 ) );
  169: +		directions.add( new Point( 5, 1 ) );
  170: +		directions.add( new Point( 7, 1 ) );
  171: +		directions.add( new Point( 1, 2 ) );
  172: +		int validCount = 1;
  173: +		for ( Point slope : directions )
  174: +		{
  175: +			validCount *= treesInPath( fileLines, slope );
  176: +		}
  177: +		System.out.println( here +"input has "+ validCount +" valid trees" );
  178: +	}
  179: +
  180: +
  181: +	private static int treesInPath(
  182: +			List<String> map,
  183: +			Point slope
  184: +	) {
  185: +		final String here = "e20031.m ";
  186: +		int trees = 0, times = 500;
  187: +		for ( Point ind = new Point( 0, 0 );
  188: +				ind.y < map.size() && times > 0;
  189: +				ind = nextPosition( map, ind, slope ) )
  190: +		{
  191: +			char collideWith = map.get( ind.y ).charAt( ind.x );
  192: +			if ( collideWith == tree )
  193: +			{
  194: +				trees += 1;			
  195: +	System.out.println( here +"tree at "+ ind );
  196: +			}
  197: +			times--;
  198: +		}
  199: +if ( times == 0 )
  200: +	System.out.println( here +"quit by count, " );
  201: +		return trees;
  202: +	}
  203: +
  204: +
  205: +	private static Point nextPosition(
  206: +			List<String> map,
  207: +			Point ind,
  208: +			Point slope
  209: +	) {
  210: +		int nextY = ind.y + slope.y;
  211: +		int nextX = ind.x + slope.x;
  212: +		if ( nextX >= map.get( 0 ).length() )
  213: +		{
  214: +			nextX = nextX - map.get( 0 ).length();
  215: +		}
  216: +		ind.setLocation( nextX, nextY );
  217: +		return ind;
  218: +	}
  219: +
  220: +}
  221: +
  222: +
  223: +
  224: +
  225: +
  226: +
  227: +
  228: +
  229: +
  230: +
  231: +
  232: +
  233: +
  234: +
  235: +
  236: +
  237: +
  238: +
  239: +
  240: +
  241: +
  242: +
  243: +
  244: +
  245: +
  246: +
  247: +
  248: diff --git a/src/res/20_03_example.txt b/src/res/20_03_example.txt
  249: new file mode 100644
  250: index 0000000..8f551de
  251: --- /dev/null
  252: +++ b/src/res/20_03_example.txt
  253: @@ -0,0 +1,11 @@
  254: +..##.......
  255: +#...#...#..
  256: +.#....#..#.
  257: +..#.#...#.#
  258: +.#...##..#.
  259: +..#.##.....
  260: +.#.#.#....#
  261: +.#........#
  262: +#.##...#...
  263: +#...##....#
  264: +.#..#...#.#
  265: \ No newline at end of file
  266: diff --git a/src/res/20_03_input.txt b/src/res/20_03_input.txt
  267: new file mode 100644
  268: index 0000000..1c7e862
  269: --- /dev/null
  270: +++ b/src/res/20_03_input.txt
  271: @@ -0,0 +1,323 @@
  272: +.........###......#...#.......#
  273: +.#.#...........#..#..#.........
  274: +#.......#.................#....
  275: +.........#.#.........#......###
  276: +.....#......##...##............
  277: +...##...#......#.....#.....##..
  278: +#.#..#....#...#....#......#....
  279: +........##.....#.....#.#.......
  280: +......#.....#......#...##.#....
  281: +...####...#.......##.....#.#...
  282: +.........#....#......##........
  283: +..##...........###.#...........
  284: +.....#............#............
  285: +#.#..#..##........#.....#..#...
  286: +.....................#....##..#
  287: +...........##.....###...#.#.#..
  288: +..#......#...........#.........
  289: +.##.##...#...#......##.#.......
  290: +......#..#......#.#.#..#.#.....
  291: +........#.#..#..........#...#.#
  292: +...........##...........#....#.
  293: +...........#...##.#............
  294: +.......#...........#...........
  295: +.......#......#..#...#....#..#.
  296: +..#.....#.#....#.#......#...#.#
  297: +.#..........###..#....#........
  298: +........##..#..#...#..#....#..#
  299: +#..........#...#..#.#........#.
  300: +..#.#........##.##....##.#.....
  301: +#.##....#...#.......#.#..#....#
  302: +......##...#.#.#.#.....#....#..
  303: +..........#..............#.....
  304: +....................###.#......
  305: +#.....#...#...#.#.......#....#.
  306: +.......#..#...................#
  307: +........##.##........#......#..
  308: +...#...##.#...#...........#....
  309: +..#.........#...#....##......#.
  310: +......#..............#..#..#.#.
  311: +.....##...#...#...##....#......
  312: +#.#....#...#......##.....#...##
  313: +.#...#.#..................#....
  314: +#.##.....#......#..........#...
  315: +..#..#.......#.................
  316: +..#.....#.........#........#...
  317: +.......#...##.##.#..#..##.#..#.
  318: +#.............#.........#.#....
  319: +..#..##..........#..#..##.#.#..
  320: +.#......#.......#...#.....##.#.
  321: +.....#......#...#...........###
  322: +..........#.........#.....#....
  323: +.....#..........#.......##...#.
  324: +......#..#..#..............#...
  325: +.#.####..#...##...#.#..........
  326: +..#....#.......#........#.....#
  327: +....#.##.....#..#.....#.#.#..#.
  328: +.......#..#..##.......#........
  329: +.#.....#...........#.....#.....
  330: +........#..........##..##.#....
  331: +.#.....#........#.....#..#.....
  332: +..#..........#...#......##..#..
  333: +.#............#.........#....#.
  334: +........#..###.......#.....###.
  335: +##.#...#.#..#..#..#.#.##...#...
  336: +.#....#...#..#......##.........
  337: +.............##.....##.........
  338: +.....##.#..###.#....#...#...#.#
  339: +#........#...#......#...#.##...
  340: +#....#......#.....###.##.#.....
  341: +.....#..#.#.##....#..##.....##.
  342: +....#...#...#..........##......
  343: +..........#......#...#.....##..
  344: +.....##....##.#.............#.#
  345: +#.........#.##.............#..#
  346: +.....#.........##.#...#.#.#....
  347: +..........#..#......#..#.....#.
  348: +....#....#....#....#.......###.
  349: +....#...#..##....#..##..#...##.
  350: +.###......#...........###......
  351: +#..................####.#....#.
  352: +#....#.#.....#.#....#..#.......
  353: +...#......#....##......#..#..#.
  354: +#.#...#.##.....#.#.......##..#.
  355: +.........##.................#..
  356: +#..##.#....#.#.............#...
  357: +....................####.#.#..#
  358: +.......#..#...#..#..#.....#...#
  359: +.....#.#.#........#....#...##..
  360: +......#..#....#......#..##.....
  361: +............#......##.#....#..#
  362: +...#..........#..#...........#.
  363: +..........#.............###....
  364: +....##.#.#......#.#..#....##..#
  365: +..#..........#........#......#.
  366: +..#...........####......##..#.#
  367: +...##......##...#..#.##........
  368: +.....#...#.....##.....###..##..
  369: +.#.##.....#....##....#.........
  370: +#....##..#.....#.#......#.#....
  371: +..#.......#...#....#...#.#.....
  372: +...........#.........#.........
  373: +..#..#....##..#....#....#.....#
  374: +.......#..#....##....#.........
  375: +#.........#...........##....##.
  376: +#........#.#...............##..
  377: +#...##.#...............#.......
  378: +#....#..#......#..#.###...##..#
  379: +..#.........#.#......#.....#..#
  380: +......#...........##........##.
  381: +.#.........#................#..
  382: +#...#...............#...#....#.
  383: +.#.#......##.........#.#.......
  384: +..........#....................
  385: +.#.....#..#...#.#.#.......#...#
  386: +..#..........#.................
  387: +.#.#.....#.#......#...#.....##.
  388: +.....#.#..##...##..#..###...#..
  389: +......#......#.#......#.##.....
  390: +#.#......#...#.......#....#....
  391: +..........#....#.#..#.....##...
  392: +#...........#.#....#.##....#.#.
  393: +#.#....#..#.........###....#...
  394: +..............#..##.......#....
  395: +.......................#.##.#..
  396: +##...............##....#..#.#..
  397: +.#.#..#.##...#.............#...
  398: +...#...........#............#..
  399: +..#......#........##....#.#.##.
  400: +.#.#..#........#....#....#....#
  401: +.#.....#.##....#.....#..#...#..
  402: +......#...#..........#..###....
  403: +..#.#.......#........#........#
  404: +.......##.####..........#......
  405: +.#.#..#......##..#.........#..#
  406: +..#...##.#.......#...#.##...#.#
  407: +#.#..........#..#.#.#..........
  408: +.....#......#............#.....
  409: +........###...#.......#........
  410: +.....#.##....#....#............
  411: +...#.#....##.....#.....#.......
  412: +..#.............#......#.......
  413: +.#....#...#....##..#......#....
  414: +..#.....#.#............#.......
  415: +......#........##.........#...#
  416: +.......#........#..#.#.#...##.#
  417: +#....#...#..#.......#....##....
  418: +#...##.#.#.....#.......#.......
  419: +.....#........#.#.....#...##...
  420: +..#....#..##......#.#.....#...#
  421: +....#.....#......#.....#.......
  422: +#.#....#......#...##...........
  423: +..#.......#...#...............#
  424: +........#........#.............
  425: +#.#.#......#...#..#..........#.
  426: +.##...#.........#........#..#..
  427: +#.#.#...#.#.......#.....#...#..
  428: +...#..............#..........#.
  429: +#.#...#.###.............#......
  430: +................#.....###.##.#.
  431: +.......#..........#....#..#....
  432: +......##....#..#..##...........
  433: +...#...#.....######.......#....
  434: +..##.....##.#...#.........#.#.#
  435: +.......#...#..#.#.#...........#
  436: +........###.............#...#.#
  437: +#.....#.........#.............#
  438: +..#...#.....#..................
  439: +.....#....#.....#......#.#....#
  440: +...#....#........##...#.......#
  441: +...##.#...#.....#..............
  442: +..#.##....##..#.........#......
  443: +.....#..#.#....#...#......#.#..
  444: +...........##..##...#..#..###..
  445: +#...........#.........####....#
  446: +.#...........#...........###...
  447: +........#................#.....
  448: +.....#....#............#....#.#
  449: +...#...#.......#...............
  450: +#.....##.#.......#.#...........
  451: +#.......#.#.#.#..#...#.........
  452: +....####.#...#.#......#.....##.
  453: +...##...#.....#.#......#..#....
  454: +..........#..#....#......###...
  455: +...................#....##...#.
  456: +....#......#........#...##..#..
  457: +##...#.........#.#......#......
  458: +#........#...#....#......#.....
  459: +#..#.......#...............##..
  460: +......##......#...........##.#.
  461: +......#..#....#....#.##........
  462: +..#....#..#.#.###....#.........
  463: +.#......#..#..............#....
  464: +.#..........#...#..#.#...#.....
  465: +....#......#..#......#....#....
  466: +...##.....#............####..#.
  467: +......#.#...#....#..#...#..#.#.
  468: +......##.......................
  469: +#.##........#...........####..#
  470: +.....#......#.......#.#....#...
  471: +#.......#....#.....#....#...##.
  472: +.....#..##.#...........#..#...#
  473: +...........#.##.#.#...#.#..#...
  474: +..#.......#.#....#..#..........
  475: +...#.......##..#.............#.
  476: +....#..#....#....#...#....#....
  477: +#......#.#...##..........#..#..
  478: +..#.#.......#.........#......#.
  479: +#...............#.............#
  480: +....##..#......................
  481: +.##....#............#......#...
  482: +.......#....#..##......##......
  483: +#..##.....#..#..........#......
  484: +...#.........#.......#..##.....
  485: +....#.##.....#.#...#...#.....#.
  486: +##...........#.#..#...#.#......
  487: +....#.............##...#.#..#..
  488: +...#....#......................
  489: +#..#...##.#.......#.##..#.###..
  490: +...##.#.#...##........##...#...
  491: +......##..#..#.....#..#.#..#...
  492: +#.......##...............##.#..
  493: +.##......#..#....#...##..#..#.#
  494: +##.........##..#...#.....##....
  495: +...#..........#...#..##.#......
  496: +..##.#........#...#..........##
  497: +.......................##.#....
  498: +....#...#...#..###.#.......#.##
  499: +....#....#.#..........#.##.....
  500: +..#..........##...#....#.......
  501: +.....#.....#.....#..#.........#
  502: +..##..##..#..#....#..#.......##
  503: +.............#............##...
  504: +....#.#.#.......###.........#..
  505: +...##.#..........#.#...#.#.....
  506: +.#........#..#.#.#..#..........
  507: +...##...#.....##.......#..#..#.
  508: +...#......#..#.......##.#.#....
  509: +.........#.........##........#.
  510: +.........##..................##
  511: +....##.....#................#..
  512: +....#..................##...#.#
  513: +.........#..............#......
  514: +...#......#..#..#....#..#...##.
  515: +.#.##......##...#.#......#.#...
  516: +...#.#...###....#...#.#..#....#
  517: +....#..#.......#.....#..##.#.#.
  518: +#.#.#..#.......#####.#..##..#..
  519: +#..........#.....#..#.#..#.....
  520: +.#......#...#..#.#..#..#.......
  521: +...#....##...#..........#.##.#.
  522: +#.##..#...#..................#.
  523: +......#.###..#..#..#.......#...
  524: +...#....#...#..#............###
  525: +#.........#........#.......#...
  526: +...#..#.................#....##
  527: +...#.#.............##......#...
  528: +##....#.##.............##......
  529: +#............#..#..#.....#.....
  530: +....#........#...#.....#.#...##
  531: +..#.##..#.....................#
  532: +#.#........#...#..#...#.#......
  533: +...#..#...........##.....#.....
  534: +......#.#....#..##...#.....#...
  535: +......#......#.###..##.........
  536: +....#.......#...##.##.....#....
  537: +#.....##....#........#..##.....
  538: +.#..#..#..#..#.#...#...#.......
  539: +.......##...#......#.........#.
  540: +.#..##....#.....#...........##.
  541: +.......##....#.#........#......
  542: +..#.#.#....#...................
  543: +.#...#.......#...#.#......#....
  544: +..##.##..##...........###......
  545: +#...#......#.......#...........
  546: +#....##.#.......#.........#....
  547: +.............##.#.#..#...#...#.
  548: +..##.##...........#.........##.
  549: +#.#...#..........#.#....#....##
  550: +.....#.....#..##..#............
  551: +#.........#.........#.#...##..#
  552: +...#.#.....#.........###..#....
  553: +..#.#.##.#...................#.
  554: +......####....#.......#.......#
  555: +.........#..#..#....#..##......
  556: +....#..........#...##........#.
  557: +..........#..#....#.....#....#.
  558: +.#.#.................#....#....
  559: +.......#......#.....#...##.....
  560: +....#..............#...........
  561: +###...........##.#...........#.
  562: +...####.......#...#....#.#...#.
  563: +..##.#................#........
  564: +...#..#....#.....#.....##..#...
  565: +##.#....#....##..........#.#..#
  566: +...#....#.....#................
  567: +..#...#....#..#..#.##.##..#....
  568: +....#....#.##.....#...#......#.
  569: +......#................#..#..#.
  570: +...##..#...#....#.#.....#..#...
  571: +#...#..............#.#.....#.#.
  572: +....#.........#.##...#.#....#..
  573: +..................#..##.#......
  574: +.#.....#.....#.............#..#
  575: +..........####....###..##...#..
  576: +......#........#...#......##..#
  577: +#......#.#..........#....#.#...
  578: +###................#.#....#....
  579: +#..#.##.#.............#..#.....
  580: +.....#............#.....##.....
  581: +....#.....#....#.........#.....
  582: +#..#...........###.#....#......
  583: +..#............##...#........##
  584: +..#....#..#....#.....#.......#.
  585: +..#..#.#.#.##.#..#...#.....#...
  586: +..........#..#.................
  587: +...#.#......#..##........#.....
  588: +...............#...............
  589: +#.......#.......#....#.........
  590: +#...........#....#.............
  591: +....#..#..........#....#..##...
  592: +.........#.#.#.........#......#
  593: +.....#...##.....#.#.......#...#
  594: +.........#....#...#.......#....

Generated by git2html.