some-advent-of-code: 9b79fa1b211d546a117efe5c034cc3c7a0799866
1:
2: import java.io.IOException;
3: import java.nio.file.Files;
4: import java.nio.file.InvalidPathException;
5: import java.nio.file.Path;
6: import java.nio.file.Paths;
7: import java.util.LinkedList;
8: import java.util.List;
9: import java.util.Map;
10: import java.util.TreeMap;
11:
12: public class Exercise19012
13: {
14:
15: public static void main( String args[] )
16: {
17: final String here = "e19012.m ";
18: if ( args.length < 1 )
19: {
20: throw new RuntimeException( here +"add a filename argument" );
21: }
22: String userSaysFile = args[ 0 ];
23: List<String> fileLines = new LinkedList<>();
24: try
25: {
26: Path where = Paths.get( userSaysFile );
27: fileLines = Files.readAllLines( where );
28: }
29: catch ( IOException | InvalidPathException ie )
30: {
31: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
32: return;
33: }
34: /*
35: - interpretation of spec -
36: for values in file
37: total += floor(x / 3) -2
38: */
39: long total = 0;
40: int lineNum = 0;
41: for ( String line : fileLines )
42: {
43: lineNum += 1;
44: int moduleWeight = 0;
45: try
46: {
47: moduleWeight = Integer.parseInt( line );
48: }
49: catch ( NumberFormatException nfe )
50: {
51: System.err.println( here +"handed a non int "+ line +" on "+ lineNum +" because "+ nfe );
52: continue;
53: }
54: int fuelWeight = fuelRequiredFor( moduleWeight );
55: while ( fuelWeight > 0 )
56: {
57: total += fuelWeight;
58: fuelWeight = fuelRequiredFor( fuelWeight );
59: }
60: }
61: System.out.println( here +"total is "+ total );
62: }
63:
64:
65: private static int fuelRequiredFor( int mass )
66: {
67: return mass /3 -2;
68: }
69: }
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
Generated by git2html.