some-advent-of-code: e56d54890be3f81b93fcc91a6adf528de201c213
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.HashSet;
8: import java.util.LinkedList;
9: import java.util.List;
10: import java.util.Map;
11: import java.util.Set;
12: import java.util.TreeMap;
13:
14: public class Exercise200102
15: {
16:
17: public static void main( String args[] )
18: {
19: final String here = "e20012.m ";
20: if ( args.length < 1 )
21: {
22: throw new RuntimeException( here +"add a filename argument" );
23: }
24: String userSaysFile = args[ 0 ];
25: List<String> fileLines = new LinkedList<>();
26: try
27: {
28: Path where = Paths.get( userSaysFile );
29: fileLines = Files.readAllLines( where );
30: }
31: catch ( IOException | InvalidPathException ie )
32: {
33: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
34: return;
35: }
36: /*
37: - interpretation of spec -
38: for values in file
39: save
40: for values in less than half
41: for values in more than half
42: if sum is target
43: print less * more
44: */
45: // gather
46: Set<Integer> values = new HashSet<>();
47: Integer temp;
48: int target = 2020;
49: int lineNum = 0;
50: for ( String line : fileLines )
51: {
52: lineNum += 1;
53: try
54: {
55: temp = Integer.parseInt( line );
56: if ( values.contains( temp ) )
57: {
58: System.err.println( here +"input contains duplicates, you have to handle that now" );
59: return;
60: }
61: else
62: values.add( temp );
63: }
64: catch ( NumberFormatException nfe )
65: {
66: System.err.println( here +"handed a non int "+ line +" on "+ lineNum +" because "+ nfe );
67: continue;
68: }
69: }
70: // combinatoric
71: for ( Integer uno : values )
72: {
73: for ( Integer two : values )
74: {
75: if ( uno == two )
76: continue;
77: for ( Integer san : values )
78: {
79: if ( uno == san || two == san )
80: continue;
81: else if ( (uno + two + san) == target )
82: {
83: System.err.println( here +"one is "+ uno +" nee is "+ two
84: +" trs is "+ san +" product is "+ (uno * two * san) );
85: return;
86: }
87: }
88: }
89: }
90: System.out.println( here +"input had no pair that summed to "+ target );
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:
Generated by git2html.