11//created by MCqwertz
22
3-
4- package com .github .mcqwertz .year2020 .days ;
5-
6- import com .github .mcqwertz .util .TextFileUtils ;
7-
83import java .io .FileNotFoundException ;
4+ import java .util .ArrayList ;
5+ import java .util .Scanner ;
96import java .util .function .IntPredicate ;
107import java .util .function .Supplier ;
118import java .util .stream .IntStream ;
129
1310
1411public class Day1 {
1512public static void main (String [] args ) throws FileNotFoundException {
16- Day1 day = new Day1 ();
17- Supplier <IntStream > supplier = TextFileUtils .getNumbers (1 );
18- System .out .println ("Task 1: " + day .getFirstPart (supplier ));
19- System .out .println ("Task 2: " + day .getSecondPart (supplier ));
13+ Supplier <IntStream > supplier = getInput ();
14+ System .out .println ("Task 1: " + getFirstPart (supplier ));
15+ System .out .println ("Task 2: " + getSecondPart (supplier ));
2016}
2117
2218/**
2319 * @param supplier supplier of the stream with all given numbers
2420 * @return the solution of the first problem
2521 */
26- private int getFirstPart (Supplier <IntStream > supplier ) {
27- IntPredicate isSearched = arg -> this . isContaining (supplier .get (), 2020 - arg );
22+ private static int getFirstPart (Supplier <IntStream > supplier ) {
23+ IntPredicate isSearched = arg -> isContaining (supplier .get (), 2020 - arg );
2824int result = 1 ;
2925//check for each int whether there is suitable second int
3026for (int i : supplier .get ().filter (isSearched ).toArray ()) {
@@ -36,7 +32,7 @@ private int getFirstPart(Supplier<IntStream> supplier) {
3632 * @param supplier supplier of the stream with all given numbers
3733 * @return the solution of the second problem
3834 */
39- private int getSecondPart (Supplier <IntStream > supplier ) {
35+ private static int getSecondPart (Supplier <IntStream > supplier ) {
4036int [] array = supplier .get ().toArray ();
4137//combine each number with every other number
4238for (int i : array ) {
@@ -50,12 +46,22 @@ private int getSecondPart(Supplier<IntStream> supplier) {
5046return -1 ;
5147}
5248
53- private boolean isContaining (IntStream stream , int x ) {
49+ private static boolean isContaining (IntStream stream , int x ) {
5450for (int i : stream .toArray ()) {
5551if (i == x ) {
5652return true ;
5753}
5854}
5955return false ;
6056}
57+
58+ public static Supplier <IntStream > getInput () throws FileNotFoundException {
59+ Scanner scanner = TextFileUtils .getScanner (1 );
60+ ArrayList <String > arrayList = new ArrayList <>();
61+ while (scanner .hasNextLine ()) {
62+ arrayList .add (scanner .nextLine ());
63+ }
64+ scanner .close ();
65+ return () -> arrayList .stream ().mapToInt (Integer ::parseInt );
66+ }
6167}
0 commit comments