Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package santaslist;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.HashSet;
- import java.util.Set;
- //This piece of code SHOULD return a list of unique strings with certain characteristics. The excercise was taken from
- //adventofcode.com, more specifically, from day 5. This code does NOT return the correct answer.
- public class SantasList {
- static FileReader file;
- static BufferedReader br;
- static Set<String> set = new HashSet<>();
- 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"};
- static String[] vocali = {"a", "e", "i", "o", "u"};
- public static void main(String[] args) throws FileNotFoundException, IOException {
- file = new FileReader("list/santaslist.txt");
- br = new BufferedReader(file);
- String line;
- int nice = 0;
- while ((line = br.readLine()) != null) {
- String fp = firstPass(line);
- //I tried doing the following in a single loop. The result was exactly the same, but at least it looks sorta cleaner :p
- if (fp != null) {
- String sp = secondPass(fp);
- if (sp != null) {
- String tp = thirdPass(sp);
- if (tp != null) {
- set.add(tp);
- }
- }
- }
- }
- for (String s : set) {
- nice++;
- System.out.println(s + " " + nice); //prints the final list of "good" strings with an increment.
- }
- }
- public static String firstPass(String s) {
- for (String str : letters) {
- if (s.contains(str)) {
- return s;
- }
- }
- return null;
- }
- public static String secondPass(String s) {
- if ((!s.contains("ab")
- && (!s.contains("cd"))
- && (!s.contains("pq"))
- && (!s.contains("xy")))) {
- return s;
- }
- return null;
- }
- public static String thirdPass(String s) {
- int voc = 0;
- for (String v : vocali) {
- if (s.contains(v)) {
- voc++;
- if (voc >= 3) {
- voc = 0;
- return s;
- }
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement