Author: Nicholas Prado <nmprado@nzen.ws>
Date: Tue Dec 1 07:10:46 UTC 2020
Parent: 12e2df13aeb201f9fe72111c360dcad8daada5c1
Log message:
feat 20 01 1+2 solved At first I divided the input by half. But, for the tri part sum, this just brute forces the combinatoric. Already 885 others had submitted before I did, le sigh.
1: diff --git a/src/java/Exercise200101.java b/src/java/Exercise200101.java 2: new file mode 100644 3: index 0000000..d115dc4 4: --- /dev/null 5: +++ b/src/java/Exercise200101.java 6: @@ -0,0 +1,117 @@ 7: + 8: +import java.io.IOException; 9: +import java.nio.file.Files; 10: +import java.nio.file.InvalidPathException; 11: +import java.nio.file.Path; 12: +import java.nio.file.Paths; 13: +import java.util.HashSet; 14: +import java.util.LinkedList; 15: +import java.util.List; 16: +import java.util.Map; 17: +import java.util.Set; 18: +import java.util.TreeMap; 19: + 20: +public class Exercise200101 21: +{ 22: + 23: + public static void main( String args[] ) 24: + { 25: + final String here = "e20011.m "; 26: + if ( args.length < 1 ) 27: + { 28: + throw new RuntimeException( here +"add a filename argument" ); 29: + } 30: + String userSaysFile = args[ 0 ]; 31: + List<String> fileLines = new LinkedList<>(); 32: + try 33: + { 34: + Path where = Paths.get( userSaysFile ); 35: + fileLines = Files.readAllLines( where ); 36: + } 37: + catch ( IOException | InvalidPathException ie ) 38: + { 39: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie ); 40: + return; 41: + } 42: + /* 43: + - interpretation of spec - 44: + for values in file 45: + partition into more and less than half of target 46: + for values in less than half 47: + for values in more than half 48: + if sum is target 49: + print less * more 50: + */ 51: + // gather 52: + Set<Integer> lessThanHalf = new HashSet<>(); 53: + Set<Integer> moreThanHalf = new HashSet<>(); 54: + Integer temp; 55: + int target = 2020, half = target /2; 56: + boolean seenExactlyHalfAlready = false; 57: + int lineNum = 0; 58: + for ( String line : fileLines ) 59: + { 60: + lineNum += 1; 61: + try 62: + { 63: + temp = Integer.parseInt( line ); 64: + if ( temp < half ) 65: + lessThanHalf.add( temp ); 66: + else if ( temp > half ) 67: + moreThanHalf.add( temp ); 68: + else if ( ! seenExactlyHalfAlready ) 69: + seenExactlyHalfAlready = true; // no need to save it 70: + else 71: + { 72: + System.err.println( here +"exactly half twice, squared is "+ (temp * temp) ); 73: + return; 74: + } 75: + } 76: + catch ( NumberFormatException nfe ) 77: + { 78: + System.err.println( here +"handed a non int "+ line +" on "+ lineNum +" because "+ nfe ); 79: + continue; 80: + } 81: + } 82: + // combinatoric 83: + for ( Integer lesser : lessThanHalf ) 84: + { 85: + for ( Integer greater : moreThanHalf ) 86: + { 87: + if ( (lesser + greater) == target ) 88: + { 89: + System.err.println( here +"lesser is "+ lesser +" greater is "+ greater +" product is "+ (lesser * greater) ); 90: + return; 91: + } 92: + } 93: + } 94: + System.out.println( here +"input had no pair that summed to "+ target ); 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: + 123: + 124: diff --git a/src/java/Exercise200102.java b/src/java/Exercise200102.java 125: new file mode 100644 126: index 0000000..e56d548 127: --- /dev/null 128: +++ b/src/java/Exercise200102.java 129: @@ -0,0 +1,119 @@ 130: + 131: +import java.io.IOException; 132: +import java.nio.file.Files; 133: +import java.nio.file.InvalidPathException; 134: +import java.nio.file.Path; 135: +import java.nio.file.Paths; 136: +import java.util.HashSet; 137: +import java.util.LinkedList; 138: +import java.util.List; 139: +import java.util.Map; 140: +import java.util.Set; 141: +import java.util.TreeMap; 142: + 143: +public class Exercise200102 144: +{ 145: + 146: + public static void main( String args[] ) 147: + { 148: + final String here = "e20012.m "; 149: + if ( args.length < 1 ) 150: + { 151: + throw new RuntimeException( here +"add a filename argument" ); 152: + } 153: + String userSaysFile = args[ 0 ]; 154: + List<String> fileLines = new LinkedList<>(); 155: + try 156: + { 157: + Path where = Paths.get( userSaysFile ); 158: + fileLines = Files.readAllLines( where ); 159: + } 160: + catch ( IOException | InvalidPathException ie ) 161: + { 162: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie ); 163: + return; 164: + } 165: + /* 166: + - interpretation of spec - 167: + for values in file 168: + save 169: + for values in less than half 170: + for values in more than half 171: + if sum is target 172: + print less * more 173: + */ 174: + // gather 175: + Set<Integer> values = new HashSet<>(); 176: + Integer temp; 177: + int target = 2020; 178: + int lineNum = 0; 179: + for ( String line : fileLines ) 180: + { 181: + lineNum += 1; 182: + try 183: + { 184: + temp = Integer.parseInt( line ); 185: + if ( values.contains( temp ) ) 186: + { 187: + System.err.println( here +"input contains duplicates, you have to handle that now" ); 188: + return; 189: + } 190: + else 191: + values.add( temp ); 192: + } 193: + catch ( NumberFormatException nfe ) 194: + { 195: + System.err.println( here +"handed a non int "+ line +" on "+ lineNum +" because "+ nfe ); 196: + continue; 197: + } 198: + } 199: + // combinatoric 200: + for ( Integer uno : values ) 201: + { 202: + for ( Integer two : values ) 203: + { 204: + if ( uno == two ) 205: + continue; 206: + for ( Integer san : values ) 207: + { 208: + if ( uno == san || two == san ) 209: + continue; 210: + else if ( (uno + two + san) == target ) 211: + { 212: + System.err.println( here +"one is "+ uno +" nee is "+ two 213: + +" trs is "+ san +" product is "+ (uno * two * san) ); 214: + return; 215: + } 216: + } 217: + } 218: + } 219: + System.out.println( here +"input had no pair that summed to "+ target ); 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: + 249: diff --git a/src/res/20_01.txt b/src/res/20_01.txt 250: new file mode 100644 251: index 0000000..ad93f5e 252: --- /dev/null 253: +++ b/src/res/20_01.txt 254: @@ -0,0 +1,200 @@ 255: +1472 256: +1757 257: +1404 258: +1663 259: +1365 260: +1974 261: +1649 262: +1489 263: +1795 264: +1821 265: +1858 266: +1941 267: +1943 268: +1634 269: +1485 270: +1838 271: +817 272: +1815 273: +1442 274: +639 275: +1182 276: +1632 277: +1587 278: +1918 279: +1040 280: +1441 281: +1784 282: +1725 283: +1951 284: +1285 285: +285 286: +1224 287: +1755 288: +1748 289: +1488 290: +1374 291: +1946 292: +1771 293: +1809 294: +1929 295: +1621 296: +1462 297: +2001 298: +1588 299: +1888 300: +1959 301: +1787 302: +1690 303: +1363 304: +1567 305: +1853 306: +1990 307: +1819 308: +1904 309: +1458 310: +1882 311: +1348 312: +1957 313: +1454 314: +1557 315: +1471 316: +332 317: +1805 318: +1826 319: +1745 320: +1154 321: +1423 322: +1852 323: +1751 324: +1194 325: +1430 326: +1849 327: +1962 328: +1577 329: +1470 330: +1509 331: +1673 332: +1883 333: +1479 334: +1487 335: +2007 336: +1555 337: +1504 338: +1570 339: +2004 340: +978 341: +1681 342: +1631 343: +1791 344: +1267 345: +1245 346: +1383 347: +1482 348: +1355 349: +1792 350: +1806 351: +1376 352: +1199 353: +1391 354: +1759 355: +1474 356: +1268 357: +1942 358: +1936 359: +1766 360: +1233 361: +1876 362: +1674 363: +1761 364: +1542 365: +1468 366: +1543 367: +1986 368: +2005 369: +1689 370: +1606 371: +1865 372: +1783 373: +1807 374: +1779 375: +1860 376: +1408 377: +1505 378: +1435 379: +1205 380: +1952 381: +1201 382: +1714 383: +1743 384: +1872 385: +1897 386: +1978 387: +1683 388: +1846 389: +858 390: +1528 391: +1629 392: +1510 393: +1446 394: +1869 395: +1347 396: +685 397: +1478 398: +1387 399: +687 400: +1964 401: +1968 402: +1429 403: +1460 404: +1777 405: +1417 406: +1768 407: +1672 408: +1767 409: +1400 410: +1914 411: +1715 412: +1425 413: +1700 414: +1756 415: +1835 416: +1926 417: +1889 418: +1568 419: +1393 420: +1960 421: +1540 422: +1810 423: +1401 424: +1685 425: +830 426: +1789 427: +1652 428: +1899 429: +796 430: +1483 431: +1261 432: +1398 433: +1727 434: +1566 435: +1812 436: +1937 437: +1993 438: +1286 439: +1992 440: +1611 441: +1825 442: +1868 443: +1870 444: +1746 445: +1361 446: +1418 447: +1820 448: +1598 449: +1911 450: +1428 451: +1734 452: +1833 453: +1436 454: +1560 455: diff --git a/src/res/20_01_example.txt b/src/res/20_01_example.txt 456: new file mode 100644 457: index 0000000..0bb977d 458: --- /dev/null 459: +++ b/src/res/20_01_example.txt 460: @@ -0,0 +1,6 @@ 461: +1721 462: +979 463: +366 464: +299 465: +675 466: +1456 467: \ No newline at end of file