Skip to content

Instantly share code, notes, and snippets.

@roccodev
Last active December 4, 2017 13:57
Show Gist options
  • Select an option

  • Save roccodev/71dccd0846ad5635a906858785003f39 to your computer and use it in GitHub Desktop.

Select an option

Save roccodev/71dccd0846ad5635a906858785003f39 to your computer and use it in GitHub Desktop.
AdventOfCode2017/Day4
int ok = 0;
for(String s : Files.readAllLines(Paths.get(new File("res/day4").getPath()))) {
ArrayList<Integer> row = new ArrayList<Integer>();
List<String> words = new ArrayList<String>();
boolean cont = false;
for(String s1 : s.split(" ")) {
if(words.contains(s1)) {
cont = true;
break;
}
else {
words.add(s1);
}
}
if(cont) continue;
ok++;
}
System.out.println(ok);
public static void main(String args[]) throws NumberFormatException, IOException {
int ok = 0;
for(String s : Files.readAllLines(Paths.get(new File("res/day4").getPath()))) {
ArrayList<Integer> row = new ArrayList<Integer>();
List<String> words = new ArrayList<String>();
boolean cont = false;
for(String s1 : s.split(" ")) {
for(String s2 : words) {
if(isAnagram(s1, s2)) {
cont = true;
if(!words.contains(s1))
words.add(s1);
break;
}
}
words.add(s1);
}
if(cont) continue;
ok++;
}
System.out.println(ok);
}
public static boolean isAnagram(String firstWord, String secondWord) {
char[] word1 = firstWord.replaceAll("[\\s]", "").toCharArray();
char[] word2 = secondWord.replaceAll("[\\s]", "").toCharArray();
Arrays.sort(word1);
Arrays.sort(word2);
return Arrays.equals(word1, word2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment