-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay2.java
More file actions
97 lines (90 loc) · 1.94 KB
/
Day2.java
File metadata and controls
97 lines (90 loc) · 1.94 KB
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
import java.util.ArrayList;
public class Day2 {
public static void main(String[] args) {
new Day2();
}
public Day2() {
ArrayList<String> input = ReadInput.read("res/input2.txt");
partOne(input);
partTwo(input);
}
private void partOne(ArrayList<String> input) {
int totalPoints = 0;
for(String game : input) {
char opponentHand = game.split(" ")[0].charAt(0);
char myHand = game.split(" ")[1].charAt(0);
int result = calculateResult(myHand, opponentHand);
if(myHand == 'X')
totalPoints += 1;
else if(myHand == 'Y')
totalPoints += 2;
else
totalPoints += 3;
if(result == 0)
totalPoints += 3;
else if(result == 1)
totalPoints += 6;
}
System.out.println(totalPoints);
}
private void partTwo(ArrayList<String> input) {
int totalPoints = 0;
for(String game : input) {
char opponentHand = game.split(" ")[0].charAt(0);
char outcome = game.split(" ")[1].charAt(0);
if(outcome == 'X') {
if(opponentHand == 'A')
totalPoints += 3;
else if(opponentHand == 'B')
totalPoints += 1;
else
totalPoints += 2;
}
else if(outcome == 'Y') {
totalPoints += 3;
if(opponentHand == 'A')
totalPoints += 1;
else if(opponentHand == 'B')
totalPoints += 2;
else
totalPoints += 3;
}
else {
totalPoints += 6;
if(opponentHand == 'A')
totalPoints += 2;
else if(opponentHand == 'B')
totalPoints += 3;
else
totalPoints += 1;
}
}
System.out.println(totalPoints);
}
private int calculateResult(char myHand, char opponentHand) {
if(myHand == 'X') {
if(opponentHand == 'A')
return 0;
else if(opponentHand == 'B')
return -1;
else
return 1;
}
else if(myHand == 'Y') {
if(opponentHand == 'A')
return 1;
else if(opponentHand == 'B')
return 0;
else
return -1;
}
else {
if(opponentHand == 'A')
return -1;
else if(opponentHand == 'B')
return 1;
else
return 0;
}
}
}