package aoc21;

import java.util.Objects;
import java.util.Scanner;

public class Day3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String first = "000000011010";
        // String first = "00000";
        int[] onesCounts = new int[first.length()];
        int numLines = 0;
        while (in.hasNext()) {
            String line = in.next();
            if (line.startsWith("Z")) {
                break;
            }
            if (Objects.equals(line, "Z")) {
                break;
            }
            for (int i =0; i < line.length(); i++) {
                if (line.charAt(i) == '1') {
                    onesCounts[i]++;
                }
            }
            numLines++;
        }
        int gamma = 0;
        int epsilon = 0;
        for (int i = 0; i < onesCounts.length; i++) {
            gamma *= 2;
            epsilon *= 2;
            if (onesCounts[i] > (numLines / 2)) {
                gamma++;
            } else  {
                epsilon++;
            }
        }
        System.out.printf("gamma: %s, epsilon: %s, result: %s\n", gamma, epsilon, gamma*epsilon);
    }

}

@@ -9,16 +9,112 @@
 aoc21;%0A%0A
+import com.google.common.collect.Iterables;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A
 import j
@@ -156,16 +156,52 @@
 Scanner;
+%0Aimport java.util.stream.Collectors;
 %0A%0Apublic
@@ -308,24 +308,25 @@
 n);%0A        
+ 
 String first
@@ -354,17 +354,16 @@
       //
- 
 String f
@@ -374,24 +374,76 @@
  = %2200000%22;%0A
+        List%3CString%3E itemsSeen = new ArrayList%3C%3E();%0A
         int%5B
@@ -486,34 +486,8 @@
 )%5D;%0A
-        int numLines = 0;%0A
     
@@ -619,32 +619,65 @@
 ;%0A            %7D%0A
+            itemsSeen.add(line);%0A
             if (
@@ -921,135 +921,401 @@
     
+%7D%0A
     
-numLines++;%0A        %7D%0A        int gamma = 0;%0A        int epsilon = 0;%0A        for (int i = 0; i %3C onesCounts.length; i++) %7B
+    List%3CString%3E oxygenList = new ArrayList%3C%3E(itemsSeen);%0A        for (int i = 0; (i %3C first.length()) && (oxygenList.size() %3E 1); i++) %7B%0A            char matchBit = (countOnes(oxygenList, i) %3E= (oxygenList.size() / 2.0)) ? '1' : '0';%0A            final int finalI = i;%0A            oxygenList = oxygenList.stream().filter(li -%3E li.charAt(finalI) == matchBit).collect(Collectors.toList());
 %0A   
@@ -1323,146 +1323,401 @@
     
+%7D%0A
     
-gamma *= 2;%0A            epsilon *= 2;%0A            if (onesCounts%5Bi%5D
+    List%3CString%3E carbonList = new ArrayList%3C%3E(itemsSeen);%0A        for (int i = 0; (i %3C first.length()) && (carbonList.size() %3E 1); i++) %7B%0A            char matchBit = (countOnes(carbonList, i)
  %3E
+=
  (
-numLines
+carbonList.size()
  / 2
+.0
 )) 
-%7B%0A                gamma++;%0A            %7D else  %7B
+? '0' : '1';%0A            final int finalI = i;%0A            carbonList = carbonList.stream().filter(li -%3E li.charAt(finalI) == matchBit).collect(Collectors.toList());
 %0A   
@@ -1721,24 +1721,26 @@
         
+%7D%0A
         
 epsilon+
@@ -1735,138 +1735,497 @@
     
-epsilon++;%0A            %7D%0A        %7D%0A        System.out.printf(%22gamma: %25s, epsilon: %25s, result: %25s%5Cn%22, gamma, epsilon, gamma*epsilon
+System.out.println(oxygenList);%0A        int oxy = Integer.parseInt(Iterables.getOnlyElement(oxygenList), 2);%0A        System.out.println(oxy);%0A        int carb = Integer.parseInt(Iterables.getOnlyElement(carbonList), 2);%0A        System.out.println(carbonList);%0A        System.out.println(carb);%0A%0A        System.out.println(oxy*carb);%0A%0A    %7D%0A%0A    private static int countOnes(List%3CString%3E oxygenMatch, int i) %7B%0A        return (int) oxygenMatch.stream().filter(s -%3E s.charAt(i) == '1').count(
 );%0A 

day 3 2