-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.java
116 lines (104 loc) · 2.58 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package day2;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class Day2 {
public static void main(String[] argv) {
try {
String fileName = "input.txt";
Path path = Paths.get(fileName);
Files.readAllBytes(path);
List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
int result = 0;
for(String game : allLines) {
char yourMove = game.charAt(2);
char opponentMove = game.charAt(0);
result += movePoints(yourMove) + gamePoints(yourMove, opponentMove);
}
System.out.println(result);
result = 0;
for(String game : allLines) {
char opponentMove = game.charAt(0);
char yourMove = calculateMove(opponentMove, game.charAt(2));
result += movePoints(yourMove) + gamePoints(yourMove, opponentMove);
}
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private static char calculateMove(char opponentMove, char outcome) {
return switch (outcome) {
case 'X' -> {
if(opponentMove == 'A') {
yield 'Z';
} else if(opponentMove == 'B') {
yield 'X';
} else {
yield 'Y';
}
}
case 'Y' -> {
if(opponentMove == 'A') {
yield 'X';
} else if(opponentMove == 'B') {
yield 'Y';
} else {
yield 'Z';
}
}
case 'Z' -> {
if(opponentMove == 'A') {
yield 'Y';
} else if(opponentMove == 'B') {
yield 'Z';
} else {
yield 'X';
}
}
default -> '0';
};
}
private static int gamePoints(char yourMove, char opponentMove) {
return switch (yourMove) {
case 'X' -> {
if(opponentMove == 'A') {
yield 3;
} else if(opponentMove == 'B') {
yield 0;
} else {
yield 6;
}
}
case 'Y' -> {
if(opponentMove == 'A') {
yield 6;
} else if(opponentMove == 'B') {
yield 3;
} else {
yield 0;
}
}
case 'Z' -> {
if(opponentMove == 'A') {
yield 0;
} else if(opponentMove == 'B') {
yield 6;
} else {
yield 3;
}
}
default -> 0;
};
}
private static int movePoints(char yourMove) {
return switch (yourMove) {
case 'X' -> 1;
case 'Y' -> 2;
case 'Z' -> 3;
default -> 0;
};
}
}