-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.cs
78 lines (69 loc) · 1.99 KB
/
Day1.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
namespace KingdomOfAlgorithmia
{
public static class Day1
{
private static string p1 = "d1p1.txt";
private static string p2 = "d1p2.txt";
private static string p3 = "d1p3.txt";
public static void Solve()
{
Console.WriteLine($"Part 1: {Part1(p1)}");
Console.WriteLine($"Part 2: {Part2(p2)}");
Console.WriteLine($"Part 3: {Part3(p3)}");
}
private static int Part1(string input)
{
return new StreamReader(input)
.ReadToEnd()
.TrimEnd()
.ToCharArray()
.Aggregate(0, (acc, chr) => acc + legend[chr]);
}
private static int Part2(string input)
{
return Battle(input, 2);
}
private static int Part3(string input)
{
return Battle(input, 3);
}
private static int Battle(string input, int steps)
{
return new StreamReader(input)
.ReadToEnd()
.TrimEnd()
.Chunk(steps)
.Aggregate(0, (acc, chunk) => acc + PotionCount(chunk));
}
private static int PotionCount(char[] chars)
{
int enemyCount = 0;
int result = 0;
foreach (char c in chars)
{
if (c != 'x')
{
enemyCount++;
result += legend[c];
}
}
switch (enemyCount)
{
case 2:
result += 2;
break;
case 3:
result += 6;
break;
}
return result;
}
private static readonly Dictionary<char, int> legend = new Dictionary<char, int>
{
{ 'A', 0 },
{ 'B', 1 },
{ 'C', 3 },
{ 'D', 5 }
};
}
}