Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author /u/Philboyd_Studge on 12/4/2015.
- */
- public class Advent5 {
- public static final String VOWELS = "aeiou";
- public static final String[] BAD = { "ab", "cd", "pq", "xy"};
- public static boolean twiceNoOverlap(String in) {
- for (int i = 0; i < in.length() - 3; i++) {
- String pair = in.substring(i,i+2);
- for (int j = i+2; j < in.length()-1;j++) {
- String temp = in.substring(j,j+2);
- if (pair.equals(temp)) return true;
- }
- }
- return false;
- }
- public static boolean isBad(String in) {
- for (String each : BAD) {
- if (in.contains(each)) return true;
- }
- return false;
- }
- public static boolean hasDouble2(String in) {
- for (int i=0;i<in.length() - 2;i++) {
- if (in.charAt(i)==in.charAt(i+2)) return true;
- }
- return false;
- }
- public static boolean hasDouble1(String in) {
- for (int i=0;i<in.length() - 1;i++) {
- if (in.charAt(i)==in.charAt(i+1)) return true;
- }
- return false;
- }
- public static int countVowels(String in) {
- int count = 0;
- for (int i = 0; i < in.length(); i++) {
- if (VOWELS.contains(in.substring(i, i+1))) count++;
- }
- return count;
- }
- public static void main(String[] args) {
- List<String> list = new ArrayList<>();
- try (BufferedReader br = new BufferedReader(new FileReader("advent4.txt"))) {
- String input = br.readLine();
- while (input != null) {
- list.add(input);
- input = br.readLine();
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- int nice = 0;
- boolean part1 = true; // false for part2
- for (String each : list) {
- if (part1) {
- if (countVowels(each) >= 3 && hasDouble1(each) & !isBad(each)) nice++;
- } else {
- if (twiceNoOverlap(each) && hasDouble2(each)) nice++;
- }
- }
- System.out.println(nice);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment