Guest User

Untitled

a guest
Dec 5th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. /**
  8.  * @author /u/Philboyd_Studge on 12/4/2015.
  9.  */
  10. public class Advent5 {
  11.  
  12.     public static final String VOWELS = "aeiou";
  13.     public static final String[] BAD = { "ab", "cd", "pq", "xy"};
  14.  
  15.     public static boolean twiceNoOverlap(String in) {
  16.         for (int i = 0; i < in.length() - 3; i++) {
  17.             String pair = in.substring(i,i+2);
  18.             for (int j = i+2; j < in.length()-1;j++) {
  19.                 String temp = in.substring(j,j+2);
  20.                 if (pair.equals(temp)) return true;
  21.             }
  22.         }
  23.         return false;
  24.     }
  25.  
  26.     public static boolean isBad(String in) {
  27.         for (String each : BAD) {
  28.             if (in.contains(each)) return true;
  29.         }
  30.         return false;
  31.     }
  32.  
  33.     public static boolean hasDouble2(String in) {
  34.         for (int i=0;i<in.length() - 2;i++) {
  35.             if (in.charAt(i)==in.charAt(i+2)) return true;
  36.         }
  37.         return false;
  38.     }
  39.     public static boolean hasDouble1(String in) {
  40.         for (int i=0;i<in.length() - 1;i++) {
  41.             if (in.charAt(i)==in.charAt(i+1)) return true;
  42.         }
  43.         return false;
  44.     }
  45.  
  46.     public static int countVowels(String in) {
  47.         int count = 0;
  48.         for (int i = 0; i < in.length(); i++) {
  49.             if (VOWELS.contains(in.substring(i, i+1))) count++;
  50.         }
  51.         return count;
  52.     }
  53.     public static void main(String[] args) {
  54.         List<String> list = new ArrayList<>();
  55.  
  56.         try (BufferedReader br = new BufferedReader(new FileReader("advent4.txt"))) {
  57.             String input = br.readLine();
  58.             while (input != null) {
  59.                 list.add(input);
  60.                 input = br.readLine();
  61.             }
  62.         } catch (IOException ioe) {
  63.             ioe.printStackTrace();
  64.         }
  65.  
  66.         int nice = 0;
  67.         boolean part1 = true; // false for part2
  68.  
  69.         for (String each : list) {
  70.             if (part1) {
  71.                 if (countVowels(each) >= 3 && hasDouble1(each) & !isBad(each)) nice++;
  72.             } else {
  73.                 if (twiceNoOverlap(each) && hasDouble2(each)) nice++;
  74.             }
  75.  
  76.         }
  77.         System.out.println(nice);
  78.     }
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment