some-advent-of-code: 1b66c53032a5657d02dfe2a2ebaa3226cc93315c
1:
2: import java.awt.Point;
3: import java.io.IOException;
4: import java.nio.file.Files;
5: import java.nio.file.InvalidPathException;
6: import java.nio.file.Path;
7: import java.nio.file.Paths;
8: import java.util.HashSet;
9: import java.util.LinkedList;
10: import java.util.List;
11: import java.util.Map;
12: import java.util.Set;
13: import java.util.TreeMap;
14:
15: public class Exercise200302
16: {
17: private static final char tree = '#', ground = '.';
18:
19: public static void main( String args[] )
20: {
21: final String here = "e20011.m ";
22: if ( args.length < 1 )
23: {
24: throw new RuntimeException( here +"add a filename argument" );
25: }
26: String userSaysFile = args[ 0 ];
27: List<String> fileLines = new LinkedList<>();
28: try
29: {
30: Path where = Paths.get( userSaysFile );
31: fileLines = Files.readAllLines( where );
32: }
33: catch ( IOException | InvalidPathException ie )
34: {
35: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
36: return;
37: }
38: /*
39: - interpretation of spec -
40: */
41: List<Point> directions = new LinkedList<>();
42: directions.add( new Point( 1, 1 ) );
43: directions.add( new Point( 3, 1 ) );
44: directions.add( new Point( 5, 1 ) );
45: directions.add( new Point( 7, 1 ) );
46: directions.add( new Point( 1, 2 ) );
47: int validCount = 1;
48: for ( Point slope : directions )
49: {
50: validCount *= treesInPath( fileLines, slope );
51: }
52: System.out.println( here +"input has "+ validCount +" valid trees" );
53: }
54:
55:
56: private static int treesInPath(
57: List<String> map,
58: Point slope
59: ) {
60: final String here = "e20031.m ";
61: int trees = 0, times = 500;
62: for ( Point ind = new Point( 0, 0 );
63: ind.y < map.size() && times > 0;
64: ind = nextPosition( map, ind, slope ) )
65: {
66: char collideWith = map.get( ind.y ).charAt( ind.x );
67: if ( collideWith == tree )
68: {
69: trees += 1;
70: System.out.println( here +"tree at "+ ind );
71: }
72: times--;
73: }
74: if ( times == 0 )
75: System.out.println( here +"quit by count, " );
76: return trees;
77: }
78:
79:
80: private static Point nextPosition(
81: List<String> map,
82: Point ind,
83: Point slope
84: ) {
85: int nextY = ind.y + slope.y;
86: int nextX = ind.x + slope.x;
87: if ( nextX >= map.get( 0 ).length() )
88: {
89: nextX = nextX - map.get( 0 ).length();
90: }
91: ind.setLocation( nextX, nextY );
92: return ind;
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:
121:
122:
Generated by git2html.