-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.cs
106 lines (98 loc) · 2.87 KB
/
day2.cs
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
abstract class Move {
public abstract int score(Move adversary);
}
class Paper : Move {
public override int score(Move adversary) {
if (adversary is Paper) {
return 3;
} else if (adversary is Rock) {
return 6;
}
return 0;
}
}
class Scissor : Move {
public override int score(Move adversary) {
if (adversary is Scissor) {
return 3;
} else if (adversary is Paper) {
return 6;
}
return 0;
}
}
class Rock : Move {
public override int score(Move adversary) {
if (adversary is Rock) {
return 3;
} else if (adversary is Scissor) {
return 6;
}
return 0;
}
}
class DecryptPlay {
private static Move convertToMove(string move) {
if (new[]{"A", "X"}.Contains(move)){
return new Rock();
} else if (new[]{"B", "Y"}.Contains(move)) {
return new Paper();
} else if (new[]{"C", "Z"}.Contains(move)) {
return new Scissor();
}
return null;
}
private static Move DecodeStratToMove(string strat, Move adversaryMove) {
if ("X" == strat){ //lose
return adversaryMove switch
{
Rock => new Scissor(),
Paper => new Rock(),
Scissor => new Paper(),
_ => null
};
} else if ("Y" == strat) { //draw
return adversaryMove switch
{
Rock => new Rock(),
Paper => new Paper(),
Scissor => new Scissor(),
_ => null
};
} else if ("Z"== strat) { //win
return adversaryMove switch
{
Rock => new Paper(),
Paper => new Scissor(),
Scissor => new Rock(),
_ => null
};
}
return null;
}
public static int countScore(string adversaryMove, string stratMove, bool realStrat) {
var advsMove = convertToMove(adversaryMove);
var myMove = realStrat ? DecodeStratToMove(stratMove, advsMove) : convertToMove(stratMove);
return myMove switch
{
Rock => 1 + myMove.score(advsMove),
Paper => 2 + myMove.score(advsMove),
Scissor => 3 + myMove.score(advsMove),
_ => 0
};
}
}
class Day2 {
public static void Run() {
string[] strategy = File.ReadAllLines(@"input/2");
var totalScore = 0;
var totalScoreRealStrat = 0;
foreach(var line in strategy){
var play = line.Split(" ");
totalScore += DecryptPlay.countScore(play[0], play[1], realStrat: false);
totalScoreRealStrat += DecryptPlay.countScore(play[0], play[1], realStrat: true);
}
Console.WriteLine($"Part 1: {totalScore}");
Console.WriteLine($"Part 2: {totalScoreRealStrat}");
}
}