some-advent-of-code: f0a02e3744d47cbaba7a69413a19e3741ace14db
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 Exercise200301
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: Point slope = new Point( 3, 1 );
42: int validCount = treesInPath( fileLines, slope );
43: System.out.println( here +"input has "+ validCount +" valid trees" );
44: }
45:
46:
47: private static int treesInPath(
48: List<String> map,
49: Point slope
50: ) {
51: final String here = "e20031.m ";
52: int trees = 0, times = 500;
53: for ( Point ind = new Point( 0, 0 );
54: ind.y < map.size() && times > 0;
55: ind = nextPosition( map, ind, slope ) )
56: {
57: char collideWith = map.get( ind.y ).charAt( ind.x );
58: if ( collideWith == tree )
59: {
60: trees += 1;
61: System.out.println( here +"tree at "+ ind );
62: }
63: times--;
64: }
65: if ( times == 0 )
66: System.out.println( here +"quit by count, " );
67: return trees;
68: }
69:
70:
71: private static Point nextPosition(
72: List<String> map,
73: Point ind,
74: Point slope
75: ) {
76: int nextY = ind.y + slope.y;
77: int nextX = ind.x + slope.x;
78: if ( nextX >= map.get( 0 ).length() )
79: {
80: nextX = nextX - map.get( 0 ).length();
81: }
82: ind.setLocation( nextX, nextY );
83: return ind;
84: }
85:
86: }
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
Generated by git2html.