-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.java
48 lines (38 loc) · 1.48 KB
/
Day2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.nio.file.Files;
import java.nio.file.Path;
class Day2 {
static int[] toWin = { 1, 2, 0 }; //for 0/Rock -> 1/Paper, 1/Paper -> 2/Scissors, 2/Scissors -> 0/Rock
static int[] toLose = { 2, 0, 1 }; //for 0/Rock -> 2/Scissors, 1/Paper -> 0/Rock , 2/Scissors -> 1/Paper
public static void main(String ... args) throws Exception {
var input = Files.readString(Path.of("../input/day2.txt"));
var score = input.lines()
.map(line -> game1(line))
.mapToInt(game -> score(game)).sum();
System.out.printf("part 1: %d\n", score);
var score2 = input.lines()
.map(line -> game2(line))
.mapToInt(game -> score(game)).sum();
System.out.printf("part 2: %d\n", score2);
}
static int[] game1(String line) {
return new int[] { line.charAt(0) - 'A', line.charAt(2)-'X' };
}
static int[] game2(String line) {
int shape1 = line.charAt(0)-'A';
int shape2 = switch (line.charAt(2)) {
case 'X' -> toLose[shape1];
case 'Z' -> toWin[shape1];
default -> shape1;
};
return new int[] { shape1, shape2 };
}
static int score(int[] game) {
var shapeScore = game[1] + 1;
return shapeScore + gameScore(game[0], game[1]);
}
static int gameScore(int shape1, int shape2) {
if (toWin[shape1] == shape2) return 6;
if (toLose[shape1] == shape2) return 0;
else return 3;
}
}