Skip to content

Commit 16e4a3a

Browse files
committed
Day 2: Playing Rock Paper Scissors with elves
1 parent 2301254 commit 16e4a3a

File tree

3 files changed

+2569
-8
lines changed

3 files changed

+2569
-8
lines changed
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package Day02;
22

3-
import java.util.Collections;
43
import java.util.List;
54

5+
import advent.utils.AdventFileUtils;
6+
67
public class Day02Main {
78

89
public static void main(String[] args) {
10+
RockPaperScissorer rockPaperScissorer = new RockPaperScissorer();
11+
List<String> stringList = getInput();
912

10-
System.out.println("Here we go!");
11-
12-
// read input
13-
14-
// print puzzle solutions
13+
int myTotalScore = rockPaperScissorer.playRockPaperScissors(stringList);
14+
System.out.println("The score I thought I would get: " + myTotalScore);
1515

16+
int myPredefinedScore = rockPaperScissorer.playPredefinedRockPaperScissors(stringList);
17+
System.out.println("The predefined score: " + myPredefinedScore);
1618
}
1719

18-
protected static List<Integer> getInput() {
19-
return Collections.emptyList();
20+
protected static List<String> getInput() {
21+
return AdventFileUtils.readInputIntoStringLines(Day02Main.class);
2022
}
2123
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package Day02;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* @author Inna Pirina
8+
* @since 02.12.2022
9+
*/
10+
public class RockPaperScissorer {
11+
12+
private final Map<String, Integer> elfScores = Map.of("A", 1, "B", 2, "C", 3);
13+
private final Map<String, Integer> myScores = Map.of("X", 1, "Y", 2, "Z", 3);
14+
15+
private final Map<String, Integer> winningStrategy = Map.of("A", 2, "B", 3, "C", 1);
16+
private final Map<String, Integer> losingStrategy = Map.of("A", 3, "B", 1, "C", 2);
17+
18+
private static final int WIN_SCORE = 6;
19+
private static final int DRAW_SCORE = 3;
20+
21+
private static final String WIN = "Z";
22+
private static final String DRAW = "Y";
23+
private static final String LOSE = "X";
24+
25+
public int playRockPaperScissors(List<String> rounds) {
26+
int myTotalScore = 0;
27+
for (String round : rounds) {
28+
String[] deals = round.trim().split(" ");
29+
int elf = elfScores.get(deals[0]);
30+
int me = myScores.get(deals[1]);
31+
myTotalScore += me;
32+
33+
int difference = Math.abs(elf - me);
34+
if ((me > elf && difference == 1) || (elf > me && difference == 2)) {
35+
myTotalScore += WIN_SCORE;
36+
} else if (me == elf) {
37+
myTotalScore += DRAW_SCORE;
38+
}
39+
}
40+
return myTotalScore;
41+
}
42+
43+
public int playPredefinedRockPaperScissors(List<String> rounds) {
44+
int myTotalScore = 0;
45+
for (String round : rounds) {
46+
String[] deals = round.trim().split(" ");
47+
String elf = deals[0];
48+
String me = deals[1];
49+
50+
switch (me) {
51+
case WIN -> myTotalScore += WIN_SCORE + winningStrategy.get(elf);
52+
case DRAW -> myTotalScore += DRAW_SCORE + elfScores.get(elf);
53+
case LOSE -> myTotalScore += losingStrategy.get(elf);
54+
default -> System.out.println("OH No!");
55+
}
56+
}
57+
return myTotalScore;
58+
}
59+
}

0 commit comments

Comments
 (0)