-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.cs
88 lines (82 loc) · 1.86 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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventofCode2022 {
internal static class DayTwo {
internal static long Part1(string input) {
string[] lines = input.Split('\n');
int sum = 0;
List<(RPS opponent, RPS play)> list = new List<(RPS, RPS)>();
foreach(string lin in lines) {
if (string.IsNullOrWhiteSpace(lin)) continue;
RPS op = (RPS)(lin[0] - 'A' + 1);
RPS me = (RPS)(lin[2] - 'X' + 1);
list.Add((op, me));
}
foreach(var g in list)
{
sum += (int)g.play;
sum += ResolveP1(g);
}
return sum;
}
private static int ResolveP1((RPS opponent, RPS play) g)
{
if (g.opponent == g.play) return 3;
if (g.opponent == g.play+1 || (g.opponent == RPS.ROCK && g.play == RPS.SCISSORS)) return 0;
return 6;
}
internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
List<(RPS opponent, RPS play)> list = new List<(RPS, RPS)>();
foreach (string lin in lines)
{
if (string.IsNullOrWhiteSpace(lin)) continue;
RPS op = (RPS)(lin[0] - 'A' + 1);
RPS me = (RPS)(lin[2] - 'X' + 1);
list.Add((op, me));
}
foreach (var g in list)
{
sum += GetScore(g.play);
sum += ResolveP2(g);
}
return sum;
}
private static int GetScore(RPS play)
{
switch(play)
{
case RPS.ROCK: return 0; //loss
case RPS.PAPER: return 3; //tie
case RPS.SCISSORS: return 6; //win
}
return 0;
}
private static int ResolveP2((RPS opponent, RPS play) g)
{
int v = 0;
if (g.play == RPS.PAPER) //end in tie
{
return (int)g.opponent;
}
if (g.play == RPS.SCISSORS) //end in win
{
v = ((int)g.opponent + 1);
if (v == 4) v = 1;
return v;
}
v = ((int)g.opponent - 1);
if (v == 0) v = 3;
return v;
}
private enum RPS
{
NONE,//0
ROCK,//1
PAPER,//2
SCISSORS,//3
}
}
}