Added algorithm and working solution ofr Day 2 Part 1
This commit is contained in:
parent
d7bae4b118
commit
da85a4e0bd
3 changed files with 2609 additions and 0 deletions
49
2022/day02/RockPaperScissors.java
Normal file
49
2022/day02/RockPaperScissors.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
import java.nio.file.Paths;
|
||||
import java.util.Scanner;
|
||||
import java.io.IOException;
|
||||
|
||||
public class RockPaperScissors {
|
||||
public static void main (String[] args) {
|
||||
//Variables!
|
||||
String roundMoves = new String();
|
||||
int myScore = 0;
|
||||
int winScore = 6;
|
||||
int drawScore = 3;
|
||||
int loseScore = 0;
|
||||
int rockScore = 1;
|
||||
int paperScore = 2;
|
||||
int scissorsScore = 3;
|
||||
|
||||
//Open the dang file
|
||||
try(Scanner inputStream = new Scanner(Paths.get("input.txt"));) {
|
||||
while (inputStream.hasNext()) {
|
||||
roundMoves = inputStream.nextLine();
|
||||
switch (roundMoves.charAt(2)) {
|
||||
case 'X':
|
||||
myScore = myScore + rockScore;
|
||||
if (roundMoves.charAt(0) == 'A') {myScore = myScore + drawScore;}
|
||||
else if (roundMoves.charAt(0) == 'B') {myScore = myScore + loseScore;}
|
||||
else if (roundMoves.charAt(0) == 'C') {myScore = myScore + winScore;}
|
||||
break;
|
||||
case 'Y':
|
||||
myScore = myScore + paperScore;
|
||||
if (roundMoves.charAt(0) == 'A') {myScore = myScore + winScore;}
|
||||
else if (roundMoves.charAt(0) == 'B') {myScore = myScore + drawScore;}
|
||||
else if (roundMoves.charAt(0) == 'C') {myScore = myScore + loseScore;}
|
||||
break;
|
||||
case 'Z':
|
||||
myScore = myScore + scissorsScore;
|
||||
if (roundMoves.charAt(0) == 'A') {myScore = myScore + loseScore;}
|
||||
else if (roundMoves.charAt(0) == 'B') {myScore = myScore + winScore;}
|
||||
else if (roundMoves.charAt(0) == 'C') {myScore = myScore + drawScore;}
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.printf("Your score is %d%n", myScore);
|
||||
} catch (IOException e) {
|
||||
System.out.println("IO Exception");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
60
2022/day02/day02_algo.txt
Normal file
60
2022/day02/day02_algo.txt
Normal file
|
@ -0,0 +1,60 @@
|
|||
===Notes===
|
||||
Opp
|
||||
A = Rock
|
||||
B = Paper
|
||||
C = Scissors
|
||||
|
||||
You
|
||||
X = Rock
|
||||
Y = Paper
|
||||
Z = Scissors
|
||||
|
||||
A < Y
|
||||
A = X
|
||||
A > Z
|
||||
|
||||
B < Z
|
||||
B = Y
|
||||
B > X
|
||||
|
||||
C < X
|
||||
C = Z
|
||||
C > Y
|
||||
|
||||
heckin' lotta case statements
|
||||
|
||||
===Algo===
|
||||
|
||||
Declare char variables for oppMove and myMove to hold moves read from file
|
||||
Declare int for myScore to maintain score count, init to 0
|
||||
Declare constants
|
||||
winScore = 6
|
||||
drawScore = 3
|
||||
loseScore = 0
|
||||
rockScore = 1
|
||||
paperScore = 2
|
||||
scissorsScore = 3
|
||||
Open file input.txt
|
||||
|
||||
Read first char into oppMove
|
||||
Read second char into myMove
|
||||
|
||||
Loop while file has contents
|
||||
Case select based on myMove
|
||||
myMove is X (Rock)
|
||||
add rockScore to myScore
|
||||
if oppMove is A (Rock) then add drawScore to myScore
|
||||
else if oppMove is B (Paper) then add loseScore to myScore
|
||||
else if oppMove is C (Scissors) then add winScore to myScore
|
||||
myMove is Y (Paper)
|
||||
add paperScore to myScore
|
||||
if oppMove is A (Rock) then add winScore to myScore
|
||||
else if oppMove is B (Paper) then add drawScore to myScore
|
||||
else if oppMove is C (Scissors) then add loseScore to myScore
|
||||
myMove is Z (Scissors)
|
||||
add scissorsScore to myScore
|
||||
if oppMove is A (Rock) then add loseScore to myScore
|
||||
else if oppMove is B (Paper) then add winScore to myScore
|
||||
else if oppMove is C (Scissors) then add drawScore to myScore
|
||||
End Loop
|
||||
Print myScore to screen
|
2500
2022/day02/input.txt
Normal file
2500
2022/day02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue