package aoc20;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Day9 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Queue<Long> queue = new LinkedList<>();
        Set<Long> set = new HashSet<>();
        for (int i = 0; i <= 25; i++) {
            long l = in.nextLong();
            queue.add(l);
            set.add(l);
        }
        while (true) {
            long next = in.nextLong();
            if (canAdd(set, next)) {
                long removed = queue.remove();
                set.remove(removed);
                queue.add(next);
                set.add(next);
            } else {
                System.out.println(next);
                break;
            }
        }

    }

    private static boolean canAdd(Set<Long> set, long next) {
        for (long first : set) {
            long second = next - first;
            if ((second != first) && set.contains(second)) {
                return true;
            }
        }
        return false;
    }
}

@@ -9,16 +9,44 @@
 aoc20;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -88,24 +88,47 @@
 LinkedList;%0A
+import java.util.List;%0A
 import java.
@@ -298,16 +298,67 @@
 em.in);%0A
+        List%3CLong%3E allNumbers = new ArrayList%3C%3E();%0A
         
@@ -564,33 +564,866 @@
 add(l);%0A        
-%7D
+    allNumbers.add(l);%0A        %7D%0A        long target = doPart1(in, queue, set, allNumbers);%0A        for (int i = 0; i %3C allNumbers.size(); i++) %7B%0A            long cumSum = allNumbers.get(i);%0A            int j = i + 1;%0A            while (cumSum %3C target) %7B%0A                cumSum += allNumbers.get(j);%0A                j++;%0A            %7D%0A            if (cumSum == target) %7B%0A                long min = allNumbers.get(i);%0A                long max = allNumbers.get(i);%0A                for (int ii = i; ii %3C j; ii++) %7B%0A                    min = Math.min(min, allNumbers.get(ii));%0A                    max = Math.max(max, allNumbers.get(ii));%0A                %7D%0A                System.out.println(min + max);%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A    private static long doPart1(Scanner in, Queue%3CLong%3E queue, Set%3CLong%3E set, List%3CLong%3E allNumbers) %7B
 %0A        while (
@@ -1646,32 +1646,70 @@
  set.add(next);%0A
+                allNumbers.add(next);%0A
             %7D el
@@ -1775,13 +1775,19 @@
     
-b
 re
-ak
+turn next
 ;%0A  
@@ -1800,33 +1800,32 @@
     %7D%0A        %7D%0A
-%0A
     %7D%0A%0A    priva

day 8 2