Skip to content

Instantly share code, notes, and snippets.

@Hoi15A
Last active December 2, 2015 13:18
Show Gist options
  • Save Hoi15A/891710424f5dbf55ee98 to your computer and use it in GitHub Desktop.
Save Hoi15A/891710424f5dbf55ee98 to your computer and use it in GitHub Desktop.
Solution for Parts 1 & 2 of Day 1 in Java
System.out.print("Enter the Code: ");
Scanner Input = new Scanner(System.in);
String Code = Input.nextLine();
//Part 1 of Day 1, Advent of Code
System.out.println("\nPart 1");
System.out.println("------");
int counterUp = 0;
int counterDown = 0;
int countTotal;
for( int i=0; i<Code.length(); i++ ) {
if( Code.charAt(i) == '(' ){
counterUp++;
}
else if( Code.charAt(i) == ')' ){
counterDown++;
}
}
countTotal = counterUp - counterDown;
System.out.println("If you start at floor 0, the instructions will bring you to floor: " + countTotal);
//End of Part 1
//Start of Part 2
System.out.println("\nPart 2");
System.out.println("------");
int CounterP2 = 0;
for( int j=0; j<Code.length(); j++ ){
if( Code.charAt(j) == '(' ) {
CounterP2++;
}
else if( Code.charAt(j) == ')' ){
CounterP2 = CounterP2 - 1;
}
if(CounterP2 < 0){
System.out.println("He would reach the Basement at Position: " + (j + 1));
break;
}
}
//End of Part 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment