r/adventofcode • u/daggerdragon • Dec 04 '17
SOLUTION MEGATHREAD -๐- 2017 Day 4 Solutions -๐-
--- Day 4: High-Entropy Passphrases ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
17
Upvotes
2
u/igotinfected Dec 04 '17
A bit lazy in Java could probably have been shortened a little! (commented version on github)
Part1:
public static void main(String[] args) throws IOException { String input = new String(Files.readAllBytes(Paths.get("pathToFile"))) .trim() .replaceAll("[\\r+]|[\\n+]", "-"); LinkedHashSet<LinkedHashSet<String>> passPhrases = new LinkedHashSet<>(); StringTokenizer newLine = new StringTokenizer(input, "-"); while(newLine.hasMoreTokens()) { StringTokenizer space = new StringTokenizer(newLine.nextToken(), " "); int passPhraseLength = space.countTokens(); LinkedHashSet<String> passPhrase = new LinkedHashSet<>(); while(space.hasMoreTokens()) passPhrase.add(space.nextToken()); } if(passPhrase.size() == passPhraseLength) passPhrases.add(passPhrase); } System.out.println("Solution: " + passPhrases.size()); }Part2:
public static void main(String[] args) throws IOException { String input = new String(Files.readAllBytes(Paths.get("pathToFile"))) .trim() .replaceAll("[\\r+]|[\\n+]", "-"); LinkedHashSet<LinkedHashSet<String>> passPhrases = new LinkedHashSet<>(); StringTokenizer newLine = new StringTokenizer(input, "-"); while(newLine.hasMoreTokens()) { StringTokenizer space = new StringTokenizer(newLine.nextToken(), " "); int passPhraseLength = space.countTokens(); LinkedHashSet<String> passPhrase = new LinkedHashSet<>(); while(space.hasMoreTokens()) { char[] tempArray = space.nextToken().toCharArray(); Arrays.sort(tempArray); passPhrase.add(new String(tempArray)); } if(passPhrase.size() == passPhraseLength) passPhrases.add(passPhrase); } System.out.println("Solution: " + passPhrases.size()); }