Advertisement
Zappykeyboard

Omino's santa list

Apr 2nd, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1.  
  2. package santaslist;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.HashSet;
  9. import java.util.Set;
  10.  
  11. //This piece of code SHOULD return a list of unique strings with certain characteristics. The excercise was taken from
  12. //adventofcode.com, more specifically, from day 5. This code does NOT return the correct answer.
  13. public class SantasList {
  14.  
  15.     static FileReader file;
  16.     static BufferedReader br;
  17.     static Set<String> set = new HashSet<>();
  18.     static String[] letters = {"aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv", "zz", "xx", "yy", "ww"};
  19.     static String[] vocali = {"a", "e", "i", "o", "u"};
  20.  
  21.     public static void main(String[] args) throws FileNotFoundException, IOException {
  22.  
  23.         file = new FileReader("list/santaslist.txt");
  24.         br = new BufferedReader(file);
  25.         String line;
  26.         int nice = 0;
  27.  
  28.         while ((line = br.readLine()) != null) {
  29.             String fp = firstPass(line);
  30. //I tried doing the following in a single loop. The result was exactly the same, but at least it looks sorta cleaner :p
  31.             if (fp != null) {
  32.                 String sp = secondPass(fp);
  33.                 if (sp != null) {
  34.                     String tp = thirdPass(sp);
  35.  
  36.                     if (tp != null) {
  37.                         set.add(tp);
  38.                     }
  39.                 }
  40.             }
  41.  
  42.         }
  43.  
  44.         for (String s : set) {
  45.             nice++;
  46.  
  47.             System.out.println(s + " " + nice); //prints the final list of "good" strings with an increment.
  48.  
  49.         }
  50.     }
  51.  
  52.     public static String firstPass(String s) {
  53.  
  54.         for (String str : letters) {
  55.             if (s.contains(str)) {
  56.                 return s;
  57.             }
  58.  
  59.         }
  60.         return null;
  61.  
  62.     }
  63.  
  64.     public static String secondPass(String s) {
  65.         if ((!s.contains("ab")
  66.                 && (!s.contains("cd"))
  67.                 && (!s.contains("pq"))
  68.                 && (!s.contains("xy")))) {
  69.  
  70.             return s;
  71.         }
  72.         return null;
  73.  
  74.     }
  75.  
  76.     public static String thirdPass(String s) {
  77.         int voc = 0;
  78.         for (String v : vocali) {
  79.  
  80.             if (s.contains(v)) {
  81.                 voc++;
  82.  
  83.                 if (voc >= 3) {
  84.                     voc = 0;
  85.                     return s;
  86.                 }
  87.  
  88.             }
  89.         }
  90.  
  91.         return null;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement