Add day 2 solutions
This commit is contained in:
parent
b3e470ac0e
commit
bce137f249
2 changed files with 158 additions and 0 deletions
77
day_02/java/Part1.java
Normal file
77
day_02/java/Part1.java
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
package day_02.java;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import jlib.FileSystem;
|
||||||
|
|
||||||
|
public class Part1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length < 1) {
|
||||||
|
System.out.println("Not enough args");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
Scanner s;
|
||||||
|
try {
|
||||||
|
s = FileSystem.readFile(args[0]);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("Can't find that file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer points = 0;
|
||||||
|
while (s.hasNextLine()) {
|
||||||
|
String l = s.nextLine();
|
||||||
|
Integer round = Part1.determineScore(
|
||||||
|
l.charAt(l.length() - 1),
|
||||||
|
l.charAt(0)
|
||||||
|
);
|
||||||
|
points = points + round;
|
||||||
|
}
|
||||||
|
System.out.println(points + " points");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if we win or the opponent wins in a game of Rock Paper Scissors
|
||||||
|
*
|
||||||
|
* A = Rock
|
||||||
|
* B = Paper
|
||||||
|
* C = Scissors
|
||||||
|
*
|
||||||
|
* X = Rock
|
||||||
|
* Y = Paper
|
||||||
|
* Z = Scissors
|
||||||
|
*
|
||||||
|
* Paper
|
||||||
|
* / ^
|
||||||
|
* / \
|
||||||
|
* v \
|
||||||
|
* Rock --> Scissors
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static boolean isWin(Character me, Character opponent) {
|
||||||
|
if ( me.equals(opponent) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (opponent.equals('A')) {
|
||||||
|
return me.equals('Y');
|
||||||
|
} else if (opponent.equals('B')) {
|
||||||
|
return me.equals('Z');
|
||||||
|
} else {
|
||||||
|
return me.equals('X');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer determineScore(Character me, Character opponent) {
|
||||||
|
Integer choice = (int)me - 87;
|
||||||
|
|
||||||
|
if (choice == ((int)opponent - 64)) {
|
||||||
|
return 3 + choice;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Part1.isWin(me, opponent)) {
|
||||||
|
return 6 + choice;
|
||||||
|
}
|
||||||
|
return choice;
|
||||||
|
}
|
||||||
|
}
|
||||||
81
day_02/java/Part2.java
Normal file
81
day_02/java/Part2.java
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
package day_02.java;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import jlib.FileSystem;
|
||||||
|
|
||||||
|
public class Part2 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
if (args.length < 1) {
|
||||||
|
System.out.println("Not enough args");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
Scanner s;
|
||||||
|
try {
|
||||||
|
s = FileSystem.readFile(args[0]);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("Can't find that file");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer points = 0;
|
||||||
|
while (s.hasNextLine()) {
|
||||||
|
String l = s.nextLine();
|
||||||
|
Integer round = determineScore(
|
||||||
|
l.charAt(0),
|
||||||
|
l.charAt(l.length() - 1)
|
||||||
|
);
|
||||||
|
points = points + round;
|
||||||
|
}
|
||||||
|
System.out.println(points + " points");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* A = Rock
|
||||||
|
* B = Paper
|
||||||
|
* C = Scissors
|
||||||
|
*
|
||||||
|
* X = Rock
|
||||||
|
* Y = Paper
|
||||||
|
* Z = Scissors
|
||||||
|
*
|
||||||
|
* Paper
|
||||||
|
* / ^
|
||||||
|
* / \
|
||||||
|
* v \
|
||||||
|
* Rock --> Scissors
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
public static int winAgainst(int choice) {
|
||||||
|
int value = choice + 1;
|
||||||
|
if (value > 3) {
|
||||||
|
value = 1;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
public static int loseAgainst(int choice) {
|
||||||
|
int value = choice - 1;
|
||||||
|
if (value < 1) {
|
||||||
|
value = 3;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer determineScore(Character opponent, Character desiredResult) {
|
||||||
|
int opposing = (int)opponent - 64;
|
||||||
|
switch (desiredResult) {
|
||||||
|
case 'X':
|
||||||
|
return loseAgainst(opposing);
|
||||||
|
case 'Y':
|
||||||
|
return opposing + 3;
|
||||||
|
case 'Z':
|
||||||
|
return winAgainst(opposing) + 6;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue