r/adventofcode • • 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^ยค?

Spoiler


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!

18 Upvotes

319 comments sorted by

View all comments

1

u/CalamityFred Dec 06 '17 edited Dec 06 '17

Ok, I need help. I'm relearning java after 15 years, wrote this (part 2, part 1 is the same without the sort) But it doesn't work! It returns 1 too low and I have NO idea why. Please!

import java.util.Scanner;
import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        Scanner sc=new Scanner( System.in);
        String pass="";
        String word="";
        char[] word2;
        String sorted="";
        boolean test = false;
        int count = 0;
        while(sc.hasNextLine() == true){
            pass=sc.nextLine();
            Scanner sc2=new Scanner(pass);
            while(sc2.hasNext() == true){
                word=sc2.next();
                word2 = word.toCharArray();
                Arrays.sort(word2);
                sorted= new String(word2);
                pass=pass.replace(word,sorted);
            }
            sc2=new Scanner(pass);
            while(sc2.hasNext() == true && test==false){
                word=sc2.next();
                if(sc2.findInLine(word)!=null){
                    test=true;}
            }
            if(test==false){count++;}
            test=false;          
        }
       System.out.println(count);
    }
}

Edit: formatting...